Pretty print array in Golang.
The simplest way to print an array or even interface in Golang is to write the below function.
go
func PrettyPrint(i interface{
}) string {
s, _ := json.MarshalIndent(i, "", "\t") fmt.Println(string(s)) return string(s)
}Also we can define as struct in GO.
go
package prettyprintgo
import (
"encoding/json"text
"fmt"text
)javascript
var (go
PrettyPrint prettyPrintInterface = &prettyPrint{}text
)go
type prettyPrintInterface interface {go
Print(i interface{}) stringtext
}go
type prettyPrint struct {text
}go
func (p *prettyPrint) Print(i interface{
}) string {go
s, _ := json.MarshalIndent(i, "", "\t")go
fmt.Println(string(s))go
return string(s)text
}