[Golang] Pretty Print Variable (struct, map, array, slice)


Pretty print variable (struct, map, array, slice) in Golang. The easiest way is through MarshalIndent function in Go standard encoding/json package.

import (
      "encoding/json"
      "fmt"
)

func PrettyPrint(v interface{}) (err error) {
      b, err := json.MarshalIndent(v, "", "  ")
      if err == nil {
              fmt.Println(string(b))
      }
      return
}

Run Code on Go Playground

Tested on: Go Playground