How did that make it into the v1 design?
How did that make it into the v1 design?
var slice []string
fmt.Println(slice == nil)The language just has a bad habit of confusing them some of the time, but not consistently, so you can still occasionally get bit by the difference
As someone who uses Go a lot, it's just one of those things...
slice := []string{}
fmt.Println(slice == nil) var m map[string]int
println(m == nil)There’s a simple reason: most JavaScript parsers reject null. At least in the slice case.
It's more that when building an API that adheres to a specification, whether formal or informal, if the field is supposed to be a JSON array then it should be a JSON array. Not _sometimes_ a JSON array and _sometimes_ null, but always an array. That way clients consuming the JSON output can write code consuming that array without needing to be overly defensive
Also, now that nil map is an empty object, shouldn't that extend to every nil struct that doesn't have a custom marshaller? It would be an object if it wasn't nil after all...
var m map[string]int = nil fmt.Println(m["foo"])
The language spec is also pretty clear on this; https://go.dev/ref/spec#Map_types:
> A nil map is equivalent to an empty map except that no elements may be added.