Hello. Already some time trying to 100% understand this topic. I read both Russian explanations and documentation (I pass A Tour of Go). The problem arose with this example (as with the others on the tour related to the links):

package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(vX*vX + vY*vY) } func AbsFunc(v *Vertex) float64 { return math.Sqrt(vX*vX + vY*vY) } func main() { v := Vertex{3, 4} fmt.Println(v.Abs()) fmt.Println(AbsFunc(&v)) p := &Vertex{4, 3} fmt.Println(p.Abs()) fmt.Println(AbsFunc(p)) } 

I understand this (the variables below are taken from the head and in no way relate to the example above):

  • i: = 5
  • a: = & i - output & a will give us a link to a , a will give us a link to i , * a will give us the value i .

If this is so, then (now all of the example code above) fmt.Println(AbsFunc(&v)) - why is the value passed by reference, if we need to pass v * Vertex to the function, is the value itself? Identical and here -

 fmt.Println(p.Abs()) fmt.Println(AbsFunc(p)) 

For some reason we pass the link to &Vertex{4, 3} , and not the value ...

    1 answer 1

    Because the declaration v *Vertex says that v is a pointer to Vertex . Accordingly, we must pass the pointer. And operation & nothing else, as taking the address. Those. calculating that very pointer