How can I pretty-print JSON using Go?

后端 未结 11 1620
挽巷
挽巷 2020-12-07 08:12

Does anyone know of a simple way to pretty-print JSON output in Go?

The stock http://golang.org/pkg/encoding/json/ package does not seem to include functiona

11条回答
  •  醉酒成梦
    2020-12-07 08:23

    For better memory usage, I guess this is better:

    var out io.Writer
    enc := json.NewEncoder(out)
    enc.SetIndent("", "    ")
    if err := enc.Encode(data); err != nil {
        panic(err)
    }
    

提交回复
热议问题