Re:Alan Donovan and Brian Kernighan Answer Questions on Go
by kornish on Friday November 20, 2015 @02:02PM
(#10603111)
To be fair, we don't know if `Baz` is a pointer or value receiver in the parent example. So it could: https://play.golang.org/p/fYO8jBbVQ5
Re:Alan Donovan and Brian Kernighan Answer Questions on Go
by mratzloff on Friday November 20, 2015 @02:23PM
(#10603252)
That's true, but it's pretty uncommon for a function to receive a value, and if so you either have a good reason for it or you're doing something wrong.
Re:Alan Donovan and Brian Kernighan Answer Questions on Go
by whateveracct on Friday November 20, 2015 @03:00PM
(#10603452)
You created a playground that intentionally avoids the nil ptr dereference.
Re:Alan Donovan and Brian Kernighan Answer Questions on Go
by jerf on Friday November 20, 2015 @04:11PM
(#10603907)
You can actually create meaningful implementations of methods on nil pointers.
if b == nil {
fmt.Println(":)")
} else {
fmt.Println(b.emote)
}
In this case, that's useless, but that's an artifact of the chosen example. I use it every so often in places where it happens to have meaning.Go often treats nil as a legal value, which means that some of the things you might expect to crash don't, and when used idiomatically can sometimes make for shorter code. For instance, the "length" call on slices will happily take a nil and return 0, you can "append" to a nil slice and get a slice back, etc. Ultimately it's still a language with a null in it, though. There's no non-nil pointer type.
https://play.golang.org/p/cjmflMBhF7
Why not learn the language before criticizing it?