Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Created July 3, 2016 22:39
Show Gist options
  • Save gnilchee/978b3b83f5023fd4e7e3538b52ca723b to your computer and use it in GitHub Desktop.
Save gnilchee/978b3b83f5023fd4e7e3538b52ca723b to your computer and use it in GitHub Desktop.
Pretty JSON output using golang
package main
import (
"bytes"
"encoding/json"
"log"
"os"
"fmt"
)
func main() {
type People struct {
FirstName string
LastName string
Age int
}
people := []People{
{"John", "Smith", 42},
{"Jane", "Doe", 34},
{"Janie", "Smith", 9},
{"Johnny", "Smith", 5},
}
b, err := json.Marshal(people)
if err != nil {
log.Fatal(err)
}
var out bytes.Buffer
json.Indent(&out, b, "", "\t")
out.WriteTo(os.Stdout)
fmt.Println("") //hack to get newline at end of output
}
@gnilchee
Copy link
Author

gnilchee commented Jul 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment