Search⌘ K
AI Features

Structures and JSON

Explore how to use Go struct tags to control JSON marshaling. Understand the use of omitempty to skip empty fields and the '-' tag value to exclude sensitive data from JSON output, enhancing your UNIX system programming in Go.

We'll cover the following...

Coding example

Imagine that we have a Go structure that we want to convert into a JSON record without including any empty fields—the next widget illustrates how to perform that task with the use of omitempty:

Go (1.19.0)
// Ignoring empty fields in JSON
type NoEmpty struct {
Name string `json:"username"`
Surname string `json:"surname"`
Year int `json:"creationyear,omitempty"`
}

Lastly, imagine that we have some sensitive data on some of the fields of a Go structure that we do not want to include in the JSON records. We can do ...