json.Encoder
A Go value that writes JSON to an io.Writer, such as standard output, with optional indentation for human readers.
In more detail
json.NewEncoder(os.Stdout).Encode(report) writes one value followed by a new line. SetIndent("", " ") makes it readable for people; leave it off for machines. Keep machine output on standard output and diagnostics on standard error.
Example
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
encoder.Encode(report)Related terms
- json.Marshal — The Go function that encodes a value, such as a struct or slice, into JSON bytes.
- io.Writer — The Go interface for anything you can write bytes to, such as a file, standard output, a buffer or an HTTP response.
- standard output and standard error — The two channels a program writes normal output (stdout) and error messages (stderr) to, plus stdin for input.
Taught in
- Exporting Results as JSON — Concurrent Systems Programming with Go