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
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+11.1k Golang : Roll the dice example
+18k Golang : How to log each HTTP request to your web server?
+15.3k Golang : Delete certain files in a directory
+6.5k PHP : Shuffle to display different content or advertisement
+7.9k Golang : Grayscale Image
+6.7k Golang : Skip or discard items of non-interest when iterating example
+27.4k Golang : Convert CSV data to JSON format and save to file
+9.7k Random number generation with crypto/rand in Go
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+25.3k Golang : Get current file path of a file or executable
+8.8k Golang : Gorilla web tool kit schema example