当前位置:网站首页>The usage of slice and the difference between slice and array
The usage of slice and the difference between slice and array
2022-04-23 19:21:00 【Handsome that handsome】
slice yes go A linguistic feature of , It's kind of like cpp Of vector, Variable length , Can expand space . Today I read it in detail , Make a summary .
slice It's essentially an interval , The prototype is []T, The general implementation is as follows :
type slice struct {
first *T
len int
cap int
}
What you can see is a pointer to an array , So it's changing slice Will change the value of the array .
The usage difference between and array is not too big .
- Array based creation :
var myArrary [3]int = [3]int{
1,2,3}
var mySlice []int = myArray[:2] //[first:last] The way , Make up when you are short 0
- Create directly :
mySlice = make([]int, 5)// Created a 5 The initial value is 0 The section of
mySlice = make([]int, 5, 10)// Created a 5, Capacity of 10 The section of
- function :
len() Returns the number of elements
cap() Returns the size of the container
append() New elements
mySlice = append(mySlice, 1, 2, 3) // The parameter type of the latter is actually an indefinite parameter
mySlice2 = []int{
7, 8, 9}
mySlice = append(mySlice, mySlice2...) Join in ... It means that after breaking up, it is passed into
- copy() Deep copy slice
slice1 := []int{
1, 2, 3, 4, 5}
slice2 := []int{
1, 2, 3}
copy(slice2, slice1)//copy slice Before 3 Elements to slice2
slice2 = slice1 // Will copy a reference to slice2
- Different from array
func arrayModify(array [5]int) {
newArray := array
newArray[0] = 88
}
func sliceModify(slice []int) {
newSlice := slice
newSlice[0] = 88
}
func main() {
array := [5]int{
1, 2, 3, 4, 5}
slice := []int{
1, 2, 3, 4, 5}
arrayModify(array)
sliceModify(slice)
fmt.Println(array)
fmt.Println(slice)
}
// [1 2 3 4 5]
// [88 2 3 4 5]
What you can see is that the array passes values , Does not change the value of the element , This and c/cpp Different , And though slice Value semantics , But it's itself a pointer type , So it changes the value , But it doesn't mean slice The reference , This needs attention . The problems caused by value semantics will be discussed in detail later .
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451036.html
边栏推荐
- C1000k TCP connection upper limit test 1
- Translation of audio signal processing and coding: Preface
- All table queries and comment description queries of SQL Server
- MySQL syntax collation (5) -- functions, stored procedures and triggers
- Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
- C1000k TCP connection upper limit test
- Encyclopedia of professional terms and abbreviations in communication engineering
- 深度学习——特征工程小总结
- Decompile and get the source code of any wechat applet - just read this (latest)
- Gossip: on greed
猜你喜欢
![[记录]TypeError: this.getOptions is not a function](/img/c9/0d92891b6beec3d6085bd3da516f00.png)
[记录]TypeError: this.getOptions is not a function

RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`

Switching power supply design sharing and power supply design skills diagram

深度学习——特征工程小总结

浅谈c语言指针的强制转换

Wechat video extraction and receiving file path
![[报告] Microsoft :Application of deep learning methods in speech enhancement](/img/29/2d2addd826359fdb0920e06ebedd29.png)
[报告] Microsoft :Application of deep learning methods in speech enhancement

Matlab 2019 installation of deep learning toolbox model for googlenet network

FTP、ssh远程访问及控制

2022.04.23 (lc_763_divided into letter interval)
随机推荐
Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
Openlayers 5.0 reload the map when the map container size changes
openlayers 5.0 当地图容器大小改变时,重新加载地图
Steps to build a deep learning environment GPU
[记录]TypeError: this.getOptions is not a function
c1000k TCP 连接上限测试
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
什么是消息队列
Using 8266 as serial port debugging tool
ArcMap connecting ArcGIS Server
Wechat video extraction and receiving file path
腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
SSDB foundation 2
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
Openlayers 5.0 loading ArcGIS Server slice service
为何PostgreSQL即将超越SQL Server?
Keysight has chosen what equipment to buy for you
openlayers 5.0 加载arcgis server 切片服务
Parsing headless jsonarray arrays
Speculation on the way to realize the smooth drag preview of video editing software