An in depth understanding of arrays and slices in Go
- 2020-06-03 06:45:51
- OfStack
Type 1.
An array is a value type, and when you assign one array to another, you pass a copy.
A slice is a reference type, and the array wrapped by the slice is called the underlying array of the slice.
Let's look at one piece of code
//a is 1 An array. Notice that the array is 1 A fixed length. You must specify the length when you initialize it. If you don't specify the length, you slice it
a := [3]int{1, 2, 3}
//b It's an array, yes a the 1 copy
b := a
//c It's a slice, it's a reference type, and the underlying array is a
c := a[:]
for i := 0; i < len(a); i++ {
a[i] = a[i] + 1
}
// change a The value of the later, b is a A copy of the, b The same, c Is a reference, c The value of the change
fmt.Println(a) //[2,3,4]
fmt.Println(b) //[1 2 3]
fmt.Println(c) //[2,3,4]
2. make
make can only be used
slice
,
map
and
channel
, so the following 1 code generates 1
slice
, is the reference type
s1 := make([]int, 0, 3)
for i := 0; i < cap(s1); i++ {
s1 = append(s1, i)
}
s2 := s1
for i := 0; i < len(a); i++ {
s1[i] = s1[i] + 1
}
fmt.Println(s1) //[1 2 3]
fmt.Println(s2) //[1 2 3]
3. When slice append exceeds the bounds of the underlying array
//n1 is n2 The underlying array
n1 := [3]int{1, 2, 3}
n2 := n1[0:3]
fmt.Println("address of items in n1: ")
for i := 0; i < len(n1); i++ {
fmt.Printf("%p\n", &n1[i])
}
//address of items in n1:
//0xc20801e160
//0xc20801e168
//0xc20801e170
fmt.Println("address of items in n2: ")
for i := 0; i < len(n2); i++ {
fmt.Printf("%p\n", &n2[i])
}
//address of items in n2:
//0xc20801e160
//0xc20801e168
//0xc20801e170
// right n2 perform append After the operation, n2 It goes out of the underlying array n1 the j
n2 = append(n2, 1)
fmt.Println("address of items in n1: ")
for i := 0; i < len(n1); i++ {
fmt.Printf("%p\n", &n1[i])
}
//address of items in n1:
//0xc20801e160
//0xc20801e168
//0xc20801e170
fmt.Println("address of items in n2: ")
for i := 0; i < len(n2); i++ {
fmt.Printf("%p\n", &n2[i])
}
//address of items in n2:
//0xc20803a2d0
//0xc20803a2d8
//0xc20803a2e0
//0xc20803a2e8
4. Quote "invalidation"
Implementation of deletion
slice
The last one
item
The function of
func rmLast(a []int) {
fmt.Printf("[rmlast] the address of a is %p", a)
a = a[:len(a)-1]
fmt.Printf("[rmlast] after remove, the address of a is %p", a)
}
After calling this function, you find the original
slice
It hasn't changed
func main() {
xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Printf("[main] the address of xyz is %p\n", xyz)
rmLast(xyz)
fmt.Printf("[main] after remove, the address of xyz is %p\n", xyz)
fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]
}
The printed results are as follows:
[main] the address of xyz is 0xc2080365f0
[rmlast] the address of a is 0xc2080365f0
[rmlast] after remove, the address of a is 0xc2080365f0
[main] after remove, the address of xyz is 0xc2080365f0
[1 2 3 4 5 6 7 8 9]
It's printed right here
slice
Pointer value because
slice
It's a reference type, so the pointer values are all the same, so let's print instead
slice
Look at the address
func rmLast(a []int) {
fmt.Printf("[rmlast] the address of a is %p", &a)
a = a[:len(a)-1]
fmt.Printf("[rmlast] after remove, the address of a is %p", &a)
}
func main() {
xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Printf("[main] the address of xyz is %p\n", &xyz)
rmLast(xyz)
fmt.Printf("[main] after remove, the address of xyz is %p\n", &xyz)
fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]
}
Results:
[main] the address of xyz is 0xc20801e1e0
[rmlast] the address of a is 0xc20801e200
[rmlast] after remove, the address of a is 0xc20801e200
[main] after remove, the address of xyz is 0xc20801e1e0
[1 2 3 4 5 6 7 8 9]
This time you can see
slice
When you pass in a function as an argument, you actually make a copy
slice
Because the
slice
It's a pointer, so from the point of view of the phenomenon,
slice
It's a reference type
conclusion
The above is the whole content of this article, I hope to bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.