Golang encoding/json.Indent() function example
package encoding/json
Indent appends to dst (1st parameter) an indented form of the JSON-encoded src (2nd parameter). Each element in a JSON object or array begins on a new, indented line beginning with prefix(3rd parameter) followed by one or more copies of indent(4th parameter) according to the indentation nesting. The data appended to dst does not begin with the prefix nor any indentation, and has no trailing newline, to make it easier to embed inside other formatted JSON data.
Golang encoding/json.Indent() function usage example
package main
import (
"bytes"
"encoding/json"
"fmt"
)
func main() {
dst := new(bytes.Buffer)
src := []byte(`{
"Name":"Adam Ng",
"Age":36,
"Job":"CEO"
}`)
json.Indent(dst, src, "**", "%%")
fmt.Println(dst)
}
Output :
{
**%%"Name": "Adam Ng",
**%%"Age": 36,
**%%"Job": "CEO"
**}
Reference :
Advertisement
Something interesting
Tutorials
+11.6k Golang : Transform comma separated string to slice example
+9.8k Golang : Fix - does not implement sort.Interface (missing Len method)
+21.2k Golang : Read a file into an array or slice example
+40.8k Golang : Convert []byte to image
+6.4k Golang : How to stop user from directly running an executable file?
+21.7k Golang : Strings to lowercase and uppercase example
+7.4k Golang : How to check if input string is a word?
+17.5k Golang : Set, Get and List environment variables
+29.9k Golang : Convert an image file to []byte
+10k Golang : Read until certain character to break for loop
+4.9k Golang : Launching your executable inside a console under Linux
+10.1k Golang : How to use if, eq and print properly in html template