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
+13k Golang : Get terminal width and height example
+19.6k Golang : Close channel after ticker stopped example
+10k Golang : Get escape characters \u form from unicode characters
+5.5k Golang : Stop goroutine without channel
+18.4k Golang : Read binary file into memory
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+7.7k Golang : Test if an input is an Armstrong number example
+12.8k Swift : Convert (cast) Int or int32 value to CGFloat
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+7.4k Golang : Scanf function weird error in Windows
+8k Findstr command the Grep equivalent for Windows
+10.3k Golang : Embed secret text string into binary(executable) file