Image for post
Image for post
Photo by JESHOOTS.COM on Unsplash

One of the most frustrating things with Go is how it handles empty arrays when encoding JSON. Rather than returning what is traditionally expected, an empty array, it instead returns null. For example, the following:

Outputs:

{"Items":null}

This occurs because of how the json package handles nil slices:

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

There are some proposals to amend the json package to handle nil slices:

About

Steven Nguyen

Tech enthusiast and problem solver

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store