Standard library / encoding/json

encoding/json.Encoder.SetIndent()

Set the prefix and indentation for subsequent Encode calls. Empty prefix and indent disable pretty-printing. Expected stdout: '[\n 1\n]\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) SetIndent(prefix, indent string)

Parameters & Attributes

NameTypeDescription
prefixstringPrefix added to indented output lines.
indentstringIndentation string for each nesting level.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/json"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  e := json.NewEncoder(&b)  e.SetIndent("", "  ")  must(e.Encode([]int{1}))  fmt.Print(b.String())}
Go Reference