Calling a method with a pointer receiver by an object instead of a pointer to it?

后端 未结 2 2142
猫巷女王i
猫巷女王i 2020-12-01 22:50

v is an object of Vertex, and Scale is a method for a pointer to Vertex. Then why is v.Scale(10) not wrong,

2条回答
  •  庸人自扰
    2020-12-01 23:10

    It's the Go automatic dereferencing:

    From https://golang.org/ref/spec#Method_values:

    As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv.

提交回复
热议问题