good question.
this is a system like Golan: Vertex and *Vertex are different types ,
see this illustrative example:
you can use type Vertex2 Vertex to define a new type of Vertex2 , and these are also different types:
while it works:
package main import "fmt" import "math" func main() { var myVer Vertex = Vertex{3, 4} var inter Abser = myVer
this will not work:
package main import "fmt" import "math" func main() { var myVer Vertex = Vertex{3, 4} var inter Abser = myVer
mistake:
. \ m.go: 8: cannot use myVer (Vertex type) as Abser type in destination:
Vertex does not implement Abser (Abs method is missing)
exactly the same error with your sample:
package main import "fmt" import "math" func main() { var myVer Vertex = Vertex{3, 4} var inter Abser = myVer
because the two Vertex and Vertex2 are different types.
and this also works :
package main import "fmt" import "math" func main() { var inter Abser = &Vertex{3, 4}
output:
inter: &main.Vertex{X:3, Y:4} 5
because:
A type can have a set of methods associated with it. A set of methods such as an interface is its interface. A collection of methods of any other type T consists of all methods declared with a receiver type T. A collection of methods of the corresponding pointer type * T is a collection of all methods declared with a receiver * T or T (that is, it also contains a collection of methods T) . Additional rules apply to structures containing anonymous fields, as described in the section on structure types. Any other type has an empty set of methods. In a set of methods, each method must have a unique name for a non-empty method.
A set of type methods defines the interfaces that the type implements and the methods that can be invoked using a receiver of this type.
ref: https://golang.org/ref/spec#Method_sets
user6169399
source share