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
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+14.4k Android Studio : Use image as AlertDialog title with custom layout example
+11.4k Golang : Delay or limit HTTP requests example
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+18.6k Golang : Find IP address from string
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+11.4k Golang : Concatenate (combine) buffer data example
+20.8k Golang : Convert date string to variants of time.Time type examples