2 ms·
eh, 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 field
by shp0ngle 4y ago
eh, 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.