Consider the following playground
We see that passing []*Vertex
and []Vertex
both change the underlying struct in the slice.
Is both []*Vertex
and []Vertex
storing the vertex's address?
评论:
YEPHENAS:
a_k_w:A slice is a struct consisting of a pointer to an array, a length and a capacity.
// []Vertex m = {*,len,cap} // This part gets copied on function call | | +-->[Vertex,Vertex,Vertex,Vertex] // This part does not get copied // []*Vertex k = {*,len,cap} // This part gets copied on function call | | +-->[*,*,*,*] // This part does not get copied | | | | | | | +-->Vertex | | +-->Vertex | +-->Vertex +-->Vertex
Slices are not arrays, they point to arrays. This is an array:
// [4]Vertex a = [Vertex,Vertex,Vertex,Vertex] // This part gets copied on function call
tjholowaychuk:Go is pass by value. So when you pass a type to a function, you are passing the value, which is then copied. When you pass a pointer-type to a function it is also copied. All values are copied. pointer-types are special in that you can use these types to pass state into and out of a function. then the pointer-type can be dereferenced into a type.
so in the example, you have a slice of two different types. one slice holds values of pointer-type *Vertex and the other holds values of type Vertex. You also pass the different slices into functions differently. one you are passing the pointer to a slice, and the other you are passing the slice(which is a struct containing three words: len, cap, pointer).
So to answer the question. No. The []*Vertex slice is holding pointer-type *Vertex and the []Vertex slice is holding type Vertex.
Drop your notion of reference at the portal into the Go universe. You have types and pointer-types, and these types hold values.
Hopefully this playground is a effective visual aid: https://play.golang.org/p/kEYFzTwbwR
it prints a textual representation of the data, the textual representation closely resembles how this data is stored within memory.
binaryblade:In that scenario it's effectively the same as doing:
a := Vertex{} a.X = 5
You're not getting a copy each time you assign the field, you're just moving the value 5 to X, whereas this will produce a copy first:
a := vertices[0] a.X = 5 vertices[0].X // won't be 5 :D
Same goes for the assignment in
for
though, this won't give you v.X == 5'spackage main import "fmt" type Vertex struct { X int Y int } func main() { vs := []Vertex{ {1, 1}, {1, 1}, } for _, v := range vs { v.X = 5 } fmt.Printf("%#v\n", vs) }
ardanstudios:maybe this will show the difference
Read this series.
https://www.goinggo.net/2017/05/language-mechanics-on-stacks-and-pointers.html
