5 ms·
For those Gophers reading this and sobbing silently, there may be hope: https://github.com/golang/go/issues/27589 https://github.com/golang/go/issues/27589
by inputespresso 7y ago
For those Gophers reading this and sobbing silently, there may be hope: https://github.com/golang/go/issues/27589 https://github.com/golang/go/issues/27589
- lilyball 7y agoWhy would you want to marshal a nil slice to json as non-null? That just hides the fact that you had a nil slice. In fact, that's the exact opposite problem; representing nulls as empty arrays.
- simiones 7y agoAs explained in the thread, that is because Go best practice is to treat `nil` and `[]T{}` as the same thing in every API. If you want an explicit `null` in your JSON you can use `*[]T` as your field type, and then it makes more sense. The very idea of allowing the nil value for slices seems to be very strange and inconsistent, as slices are struct types, not pointer types in Go (they contain a pointer and other members). Just one of the many ways that builtin types in Go are fundamentally different from user-creatable types, I guess...
- lilyball 7y agoIt is admittedly very weird that Go supports nil slices/maps instead of just having the nil value be an empty slice/map that points to constant storage. But as long as Go has a semantic difference internally, representing that as null externally makes sense. Though I suppose as long as the conversion from null to empty array/object is opt-in it's fine. For context, we recently had a bug where backend forgot to initialize their map, so they were sending us a null where we expected an object, and it would have gone undetected for much longer if the JSON didn't contain a literal null there.
- yencabulator 7y agoThe nil value for a slice is an empty slice that points to constant storage: Data=0, Len=0, Cap=0. That's just not the same as Data=somethingelse, ... which you get if you allocate something. (And all Go zero values are exactly what you get with the relevant RAM filled with the zero byte.)
- lilyball 7y agoIf the nil value for a slice is the empty slice, then why do the following two variable definitions differ in behavior? var x []int; y := []int{}; Both produce a slice of capacity zero, except `x` serializes to null and `y` serializes to an empty array.