I am going through the Tour of Go. I learned that if we have a method that accepts a pointer as a receiver, it will also accept a value type as a receiver (with go doing the conversion automatically).
type Vertex struct { X, Y float64 }
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X * v.X + v.Y * v.Y)
}
Then the following code is valid, whether Vertex is received by value or pointer
v := Vertex{1, 2}
fmt.Println(v.Abs())
p := &v
fmt.Println(p.Abs())
However, let's say we have the following interface:
type Abser interface {
Abs() float64
}
Then, why is the following code invalid?
var a Abser
v := Vertex{1, 2}
a = v // invalid
My understanding was that this would be fine. Even though v is a value type which "implements" the Abs function which takes a pointer receiver, it would also take it by value?
Were interfaces simply designed to be more strict in terms of what an interface variable can hold on the right hand side? The interface is seeing *Vertex and Vertex as two different types, however, the Abs() method doesn't have an issue processing either.