I am 2-days old in the world of Golang, and going through the go tour. I couldn\'t help but notice a peculiarity which I cannot seem to be able to come at terms with a
nice Question.
this is Golang type system: the Vertex
and *Vertex
are different types,
see this clarifying sample:
you can use type Vertex2 Vertex
to define new Vertex2
type and these are different types too:
while this is working:
package main
import "fmt"
import "math"
func main() {
var myVer Vertex = Vertex{3, 4}
var inter Abser = myVer //assigning Vertex type to inter
fmt.Println(inter.Abs())
}
func (v Vertex) Abs() float64 { //method with pointer receiver argument
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Vertex struct {
X, Y float64
}
type Abser interface {
Abs() float64
}
this will not work:
package main
import "fmt"
import "math"
func main() {
var myVer Vertex = Vertex{3, 4}
var inter Abser = myVer //assigning Vertex type to inter
fmt.Println(inter.Abs())
}
type Vertex2 Vertex
func (v Vertex2) Abs() float64 { //method with pointer receiver argument
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Vertex struct {
X, Y float64
}
type Abser interface {
Abs() float64
}
the error is:
.\m.go:8: cannot use myVer (type Vertex) as type Abser in assignment:
Vertex does not implement Abser (missing Abs method)
exactly same error with your sample:
package main
import "fmt"
import "math"
func main() {
var myVer Vertex = Vertex{3, 4}
var inter Abser = myVer //assigning Vertex type to inter
fmt.Println(inter.Abs())
}
func (v *Vertex) Abs() float64 { //method with pointer receiver argument
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Vertex struct {
X, Y float64
}
type Abser interface {
Abs() float64
}
because the two Vertex and Vertex2 are different types.
and this works too:
package main
import "fmt"
import "math"
func main() {
var inter Abser = &Vertex{3, 4} //assigning &Vertex type to inter
fmt.Printf("inter: %#[1]v\n", inter)
fmt.Println(inter.Abs())
}
func (v Vertex) Abs() float64 { //method with pointer receiver argument
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type Vertex struct {
X, Y float64
}
type Abser interface {
Abs() float64
}
output:
inter: &main.Vertex{X:3, Y:4}
5
because:
A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.
The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.
ref: https://golang.org/ref/spec#Method_sets