Golang encoding/json.MarshalIndent() function example
package encoding/json
MarshalIndent is like Marshal but applies Indent to format the output.
Golang encoding/json.MarshalIndent() function usage example
package main
import (
"encoding/json"
"fmt"
"os"
)
type Employee struct {
Name string
Age int
Job string
}
func main() {
worker := Employee{
Name: "Adam",
Age: 36,
Job: "CEO",
}
output, err := json.MarshalIndent(worker, "%%%%", "$$") // <--- here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(output))
}
Output :
{
%%%%$$"Name": "Adam",
%%%%$$"Age": 36,
%%%%$$"Job": "CEO"
%%%%}
Reference :
Advertisement
Something interesting
Tutorials
+11.1k Golang : Web routing/multiplex example
+4.4k Golang : Valued expressions and functions example
+22.2k Golang : Securing password with salt
+11.1k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5.7k Get website traffic ranking with Similar Web or Alexa
+41.9k Golang : How do I convert int to uint8?
+5.7k List of Golang XML tutorials
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+6.6k Golang : Warp text string by number of characters or runes example
+19.9k Golang : Count JSON objects and convert to slice/array
+29.1k Golang : Get first few and last few characters from string