nil slice vs empty slice serialized differently in JSON
Go 1.22. An API I'm writing returns a struct with a []string field. When there are no items, sometimes it serializes as null and sometimes as [], depending on how the slice was built. The frontend hates the inconsistency. Why the difference and how do I force one? The frontend team is blocked on this, so I'd prefer to fix the contract rather than paper over it on their side.
4 replies