So,
function receiver (aka method) can receive some type.
And there is a “method inderection” - one can declare to receive a pointer type,
but actual value type will work and vice versa.
However,
let us declare an interface type A with some method and lets declare this method that receives a pointer type V.
Then only a variable of pointer type V will compile successfully, and a variable of actual type V will not.
And if the method receives actual type V, then a variable of both pointer and actual types will compile
successfully with that method.
Who knows what is the reason behind this (i would say inconsistent) behavior ?
If one implements an inteface with actual type, then calling method on pointer type will derefence -> value is copied -> no harm
if one implements an inteface with pointer type, then calling method is only possible on pointer type -> explicit -> no harm
But when dealing with type itself, calling a method that receives a pointer type on an actual value,
will result in silent referencing under the hood, which is a potential spot for logical bugs.
Consider an example:
package main
import "fmt"
type MyType struct {
x int
}
func (a *MyType) inc() int {
a.x++
return a.x
}
func main() {
a := MyType{1}
a.inc()
fmt.Println(a)
}
Here we have var a mutated which is quite inevident.
So my main concern is why not to be consistent and restrict to pass exact (actual or pointer) types everywhere ?