type S struct{}
func (s *S) Test() {
if s == nil {
fmt.Println("Nil pointer")
} else {
fmt.Println("Not nil pointer")
}
}
func main () {
var p *S
p.Test()
}
That will print "Nil pointer", not crash.While I'm actually still in agreement that I don't love a language with nil pointers, it is less bad in Go because they are less pointy and stabby. It's not like C where they are automatically a crashing offense; they are valid values treated in sensible ways in a lot more places than in C, because in Go they still have their type associated with them. But I'd still like to be able to declare a non-nillable pointer type.
Please do note carefully the difference between less bad and not bad.
While it's nicer that you can perform the check inside Test() instead of before the call, I don't understand why the type system doesn't just prohibit this and make you use a union type if you want (S or nil) given that a lot more situations call for (S not nil) than (S or nil).