This is a creation in Article, where the information may have evolved or changed.
I prefer to give the code first, and then come to the conclusion
Array
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8Arr: = [...] Int{1, 2, 3}9//Print the initial pointerTenFmt. Printf ("The pointer is:%p \ n", &arr) One Printpointer (arr) A } - - func printpointer (Any interface{}) { theFmt. Printf ("The pointer is:%p \ n", &Any ) -}
Results
1 is 2 is
Slice
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8Arr: = Make ([]int, 3)9//Print the initial pointerTenFmt. Printf ("The pointer is:%p \ n", arr) One Printpointer (arr) A } - - func printpointer (Any interface{}) { theFmt. Printf ("The pointer is:%p \ n", any) -}
Results
1 is 2 is
Map
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8Arr: =Make (map[int]string)9Arr: = [3]int{1, 2, 3}Ten//Print the initial pointer OneFmt. Printf ("The pointer is:%p \ n", arr) A Printpointer (arr) - } - the func printpointer (Any interface{}) { -Fmt. Printf ("The pointer is:%p \ n", any) -}
Run results
1 is 2 is
As a result, we see that the array itself passes the value, and after it has passed through the function, it opens up another space.
Because the array is his own. This sentence seems to be not well understood.
This is the slice arr: = make ([]int, 3) and Arr itself is not an array, at least not the one we want to point to. There is only one address in arr that points to an array.
So here's an example:
Arr: = [...] int{1,2,3,4,5} This is an array that understands the go language. Arr itself is an array
Arrslice: = Arr[0:5] This is a slice. The printed values are the same, and above. The arrslice itself is not an array, but the arrslice itself has a value that is a pointer to arr.
A slice refers to a structure, which is roughly structured like this
1 struct slice{2 ptr *Elem3 len int4 Cap int 5 }
In other words, the above arrslice is actually a struct. Inside there is a property ptr pointing to an array arr
In fact, Arrslice is also passed to the function, but also copied. But despite the passing of a copy of the struct, his attribute ptr, does not change. is also a pointer to the original array.
The following example bears witness to his own passing, which is a process of copying:
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8Arrslice: = Make ([]int, 4)9Fmt. Printf ("The pointer is:%p \ n", Arrslice)TenFmt. Printf ("The pointer is:%p \ n", &arrslice)//This is the pointer to the arrslice itself, the pointer to the struct. One Printpointer (Arrslice) A } - - func printpointer (Any interface{}) { theFmt. Printf ("The pointer is:%p \ n", any) -Fmt. Printf ("The pointer is:%p \ n", &any)//prints a pointer to the arrslice of the structure passed over -}
Look at the results:
1 is 2 is 3 are 4 is
The 1th, 3 print is the PTR property that prints the struct, which is a pointer to the array.
In fact, this structure to the function, is a copy of the process, the 2nd, 4 pointers are different.
Let's take a look at the following picture: