When running the following code:
package main
import (
"fmt"
)
type Bar struct {
name string
}
func (foo Bar) testFunc() {
fmt.Println(foo.name)
}
func doTest(pointer *Bar) {
pointer.testFunc() // run `testFunc` on the pointer (even though it expects a value of type `Bar`, not `*Bar`)
}
func main() {
var baz Bar = Bar{
name: "Johnny Appleseed",
}
doTest(&baz) // send a pointer of `baz` to `doTest()`
}
The output reads: Johnny Appleseed
. I would have thought I would have encountered an error for calling testFunc()
on a pointer.
After that, I tried switching out doTest(&baz)
for &baz.testFunc()
. Then I received the error:
tmp/sandbox667065035/main.go:24: baz.testFunc() used as value
Why do I only get the error when calling baz.testFunc()
directly instead of through another function? Wouldn't calling doTest(&baz)
and &baz.testFunc()
do the exact same thing, as doTest(pointer *Bar)
simply calls pointer.testFunc()
?
It's because of automatic derefencing of 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.
For the second line, you have this error because you take the address of the result of testFunc which doesn't return any value. What you tried to do is the following:
(&baz).testFunc()
which works as expected