Slice Full analysis
Yesterday in the group of small partners to share, gave a piece of code:
package mainimport ( "fmt")func fun1(x int) { x = x + 1}func fun2(x *int) { *x = *x + 1}func fun3(x []int) { x = append(x, 3)}func fun4(x *[]int) { *x = append(*x, 3)}func fun5(x [4]int) { x[3] = 100}func fun6(x *[4]int) { (*x)[3] = 200}// 传值,传指针func main() { x1 := 10 fmt.Printf("%+v\n", x1) fun1(x1) fmt.Printf("%+v\n\n", x1) fmt.Printf("%+v\n", x1) fun2(&x1) fmt.Printf("%+v\n\n", x1) var x3 []int x3 = append(x3, 0, 1, 2) fmt.Printf("%+v\n", x3) fun3(x3) fmt.Printf("%+v\n\n", x3) fmt.Printf("%+v\n", x3) fun4(&x3) fmt.Printf("%+v\n\n", x3) var x4 [4]int for i := 0; i < 4; i++ { x4[i] = i } fmt.Printf("%+v\n", x4) fun5(x4) fmt.Printf("%+v\n\n", x4) fmt.Printf("%+v\n", x4) fun6(&x4) fmt.Printf("%+v\n\n", x4)}
Can be run on play, the actual output is
10101011[0 1 2][0 1 2][0 1 2][0 1 2 3][0 1 2 3][0 1 2 3][0 1 2 3][0 1 2 200]
The conclusion is that slice is a reference pass, the array is a value pass, but to modify the slice and arrays, you need to pass in the address of the slice or array.
The array in this conclusion is a value pass, and to modify the array values inside the calling function, you must pass the array pointers, and I have no comment. But the slice part is not that simple. Basically, the following points need to be clarified to explain the above code.
The structure of the slice is Uintptr+len+cap
Like I've defined a slice, no matter what the method defines.
var a []inta = make([]int, 1)a := []int{1,2}
The A is assigned by a fixed data structure.
This data structure has three, one is a pointer to a fixed-length array, and one is Len, which indicates that I have several values in this slice, and a cap, which indicates how much space I applied for when I applied for a fixed-length array.
Slice's append operation is to determine whether to apply for new space based on the relationship between Cap and Len
In memory view space, there is no indefinite long array, all the syntax of the indefinite long array is the language itself encapsulated. Like the slice in Golang. Slice can define how much space I need to use when initializing (CAP)
a := make([]int, 1, 10)
The 10 here, cap,1, is Len, stating that I have created 10 int spaces for this slice.
When you continue to append the data in a, the first is Len increasing, when Len and the CAP, when the time again append the data, will be a new array space, this array space length is how big? 2*cap.
For example, if the previous a followed by another append of 9 data, this time if you append another data, you will find that cap becomes 20.
Of course, if the expansion, then we say the first element of the slice, pointing to the fixed-length array of the address will change.
Understand the following code:
package mainimport "fmt"import "unsafe"func main() { var a []int a = append(a, 0) printSlice("a", a) a = append(a, 1) printSlice("a", a) a = append(a, 2, 3, 4) printSlice("a", a)}func printSlice(s string, x []int) { fmt.Printf("%s len=%d cap=%d ptr=%p %v\n",s, len(x), cap(x), unsafe.Pointer(&x[0]), x)} 输出:a len=1 cap=2 ptr=0x10414020 [0]a len=2 cap=2 ptr=0x10414020 [0 1]a len=5 cap=8 ptr=0x10458020 [0 1 2 3 4]
When the function parameter is slice, the value copy of the "Slice structure" is passed.
We say that the slice is passed as a reference pass, in fact, it passes a copy of the slice structure (UINTPTR+LEN+CAP), but because uintptr corresponds to a fixed-length array, basically when slice is passed as a parameter, The returned slice structure will not change, the size of the corresponding fixed-length array will not change, but the fixed-length array inside the specific value is likely to change.
Look at the following examples:
package mainimport ( "fmt" "unsafe")func fun3(x []int) { fmt.Printf("%p\n", unsafe.Pointer(&x[0])) x = append(x, 3) x[2] = 100 fmt.Printf("%p\n", unsafe.Pointer(&x[0]))}func main() { var x3 []int x3 = append(x3, 0, 1, 2) fmt.Printf("%+v\n", x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fun3(x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fmt.Printf("%+v\n\n", x3)}输出:[0 1 2]0x104140200x104140200x104140200x10414020[0 1 100]
The value of the x3[2] here has changed. But Slice's pointer address doesn't change.
If the value of x[3] is modified inside the F3:
package mainimport ( "fmt" "unsafe")func fun3(x []int) { fmt.Printf("%p\n", unsafe.Pointer(&x[0])) x = append(x, 3) x[3] = 100 fmt.Printf("%p\n", unsafe.Pointer(&x[0]))}func main() { var x3 []int x3 = append(x3, 0, 1, 2) fmt.Printf("%+v\n", x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fun3(x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fmt.Printf("%+v\n\n", x3)}输出:[0 1 2]0x104140200x104140200x104140200x10414020[0 1 2]
The value of the X3 here does not change, although it does not change, but in fact the value of the fixed-length array that the slice points to is 3 has changed.
What if F3 is a append two?
package mainimport ( "fmt" "unsafe")func fun3(x []int) { fmt.Printf("%p\n", unsafe.Pointer(&x[0])) x = append(x, 3, 4) x[3] = 100 fmt.Printf("%p\n", unsafe.Pointer(&x[0]))}func main() { var x3 []int x3 = append(x3, 0, 1, 2) fmt.Printf("%+v\n", x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fun3(x3) fmt.Printf("%p\n", unsafe.Pointer(&x3[0])) fmt.Printf("%+v\n\n", x3)}输出:[0 1 2]0x104140200x104140200x104580000x10414020[0 1 2]
We see that the slice-pointed address changes after append in Fun3. But since this x is actually a copy of the value of the X3 we passed in, this X3 has not been modified. The final output is still unchanged.
Summarize
Basically remember these few points to understand slice:
- The structure of the slice is Uintptr+len+cap
- Slice's append operation is to determine whether to apply for new space based on the relationship between Cap and Len
- When the function parameter is slice, the value copy of the "Slice structure" is passed.
Reference