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
+8.9k Golang : Find network service name from given port and protocol
+8.2k Golang : Metaprogramming example of wrapping a function
+7.9k Golang : Ways to recover memory during run time.
+5.9k AWS S3 : Prevent Hotlinking policy
+10k Golang : Read file and convert content to string
+18k Golang : Get all upper case or lower case characters from string example
+18k Golang : How to log each HTTP request to your web server?
+8.1k Golang : Append and add item in slice
+5.3k Swift : Convert string array to array example
+12.4k Elastic Search : Return all records (higher than default 10)
+25.3k Golang : Convert uint value to string type
+25.7k Golang : missing Mercurial command