📝 Edit page
➕ Add page
JSON
Convert to JSON
Basic
// printJSON will accept generic response, convert to JSON and print it.
func printJSON(resp interface{}) {
json, err := json.Marshal(resp)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n", string(json))
}
Indent
Based on main.go in githubv4
example section.
// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
func priprintJSONntAsJSON(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}