Re:Down the Golang Nil Rabbit Hole
by pishpash on Tuesday March 30, 2021 @03:56AM
(#26632152)
Nils are typed initial values, that should be clear from the existence of nil slices. Don't check nil on an interface type. Check in compress(), where a concrete type is established.
[ Parent
| on HN ]
Re:Down the Golang Nil Rabbit Hole
by gher-shyu3i on Tuesday March 30, 2021 @07:18AM
(#26633262)
And implement that check in each and every function that has a receiver? That sounds like a recipe for bad performance (unnecessary branching), as well as unnecessary readability affects.
Not necessarily. It's suggested as a way to maintain the typing decision the original parent made, but which itself is undesirable. The pattern can be changed to,
func main() {
var c Compressor // signal do nothing
DoCompress(c)
c = &GZipComprosser{} // normal call
DoCompress(c)
}