4 ms·
Pretty much the only time I see a nil pointer panic in Go is when a junior dev doesn't initialize a map, which is easy to diagnose and fix (and should probably
by Ferret7446 4y ago
Pretty much the only time I see a nil pointer panic in Go is when a junior dev doesn't initialize a map, which is easy to diagnose and fix (and should probably get a linter). It's a non-issue as far as I'm concerned.
- jeremyjh 4y agoJust don’t write any bugs!
- bsaul 4y agobasically any struct containing pointers can be misinitialized and have nil pointers. it is indeed quite easy to fix most of the time, but it's also a non-issue in a lot of modern PL.
- tjpnz 4y agoIt's an easy mistake to make regardless of experience.
- shp0ngle 4y agoeh, that’s dumb. Most go types have useful default value. Nil slice is a 0 length slice. Default byte.Buffer is an empty buffer. Default of struct is all fields default. That’s all great. Except for map. Which panics. Oops!
- foldr 4y agoIt's because appending to a slice returns another slice, whereas assigning a key to a map doesn't return another map. That means that there's no sensible way to assign a key of a nil map. You could make adding a key to a map work like appending a value to a slice, or you could add an extra level of indirection to the underlying map type (make it a pointer to a struct instead of a struct). Neither of those alternatives seems particularly attractive, though.
- valyala 4y agonil map in Go can be used for reading. It cannot be used for writing only.