简单记录一下go语言json格式化输出的办法
import (
"bytes"
"encoding/json"
"fmt"
"os"
)
type Complex_Type struct{
Age int `json:"age"`
Name string `json:"name"`
Grades map[string]int `json:"grade"`
Parents []string `json:"parents"`
}
grades := map[string]int{
"math" : 96,
"chinese" : 87,
"english" : 93,
}
parents := []string{
"minato",
"kushina",
}
complex_type := Complex_Type{
Age: 18,
Name: "Naruto",
Grades : grades,
Parents: parents,
}
res,err := json.Marshal(complex_type)
exitOnError(err)
var out bytes.Buffer
err = json.Indent(&out,res,"","\t")
out.WriteTo(os.Stdout)
fmt.Printf("\n")
输出:
|