Remix.run Logo
rplnt 3 days ago

Well those are different things, aren't they? Empty slice/map is different from nil. So it makes a lot of sense that nil = null and []string{} = [], and you have an option to use both. That being said, it starts to make less sense if you work with go where the API mostly treats it as equivalent (append, len, []). So that would be my guess how it ended up the way it did.

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...

int_19h 3 days ago | parent [-]

It is different from nil, but then again a nil map in Go behaves like an empty map when reading from it. If you consider serialization to be "reading", therefore, it makes sense to interpret it accordingly.

leononame a day ago | parent [-]

That is not true, though. Reading from a nil map panics, and reading from an empty map does not.

int_19h a day ago | parent [-]

It doesn't. E.g. this prints 0:

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.