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
+14.4k Elastic Search : Mapping date format and sort by date
+11.1k Golang : Create Temporary File
+11.9k Golang : Find age or leap age from date of birth example
+14.6k Golang : Parsing or breaking down URL
+12.8k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+14.8k Golang : How to check if your program is running in a terminal
+14.6k Golang : Simple word wrap or line breaking example
+14k Golang : Check if an integer is negative or positive
+14.3k Golang : Reverse IP address for reverse DNS lookup example
+38.4k Golang : Read a text file and replace certain words
+10.9k Golang : Get local time and equivalent time in different time zone
+16.9k Golang : Get own process identifier