It sounds like you’re asking about this error message:
http://play.golang.org/p/h80rmDYCTI
package main
import "fmt"
type A struct {}
type B struct {}
func (a *A) Foo() {
fmt.Println("A")
}
func (b *B) Foo() {
fmt.Println("B")
}
func main() {
n := nil
n.Foo()
}
This prints:
prog.go:17: use of untyped nil
[process exited with non-zero status]
In that example, should the program print “A” or “B”?
You have to help the compiler decide. The way you do that is by specifying the type of n
.
For example:
http://play.golang.org/p/zMxUFYgxpy
func main() {
var n *A
n.Foo()
}
prints “A”.
In other languages, n.Foo()
might crash immediately if n
is nil
or its equivalent. Go’s language designers decided to let you determine what should happen instead. If you access the pointer without checking for nil
, you get the same behavior as in other languages.