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()
?