Created
July 3, 2016 22:39
-
-
Save gnilchee/978b3b83f5023fd4e7e3538b52ca723b to your computer and use it in GitHub Desktop.
Pretty JSON output using golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it out: https://play.golang.org/p/UkrT-bMxNm