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.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+29.5k Golang : Saving(serializing) and reading file with GOB
+5.8k Linux : Disable and enable IPv4 forwarding
+7k Golang : How to call function inside template with template.FuncMap
+20.9k PHP : Convert(cast) int to double/float
+9.2k Golang : Write multiple lines or divide string into multiple lines
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+26.6k Golang : Encrypt and decrypt data with AES crypto
+8.4k Golang : Generate Datamatrix barcode
+5.2k JavaScript/JQuery : Redirect page examples
+7.3k Golang : How to iterate a slice without using for loop?
+9.6k Golang : Validate IPv6 example