Golang encoding/json.Marshal() function example
package encoding/json
Marshal returns the JSON encoding of v. ( see http://golang.org/pkg/encoding/json/#Marshal for full description )
Golang encoding/json.Marshal() 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.Marshal(worker) // <--- here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(output))
// os.Stdout.Write(b) -- also ok
}
Output :
{"Name":"Adam","Age":36,"Job":"CEO"}
Reference :
Advertisement
Something interesting
Tutorials
+19.8k Golang : Append content to a file
+15.4k Golang : invalid character ',' looking for beginning of value
+7.8k Golang : Example of how to detect which type of script a word belongs to
+7.3k Golang : alternative to os.Exit() function
+14.4k Golang : How to pass map to html template and access the map's elements
+5.3k Javascript : Shuffle or randomize array example
+17.2k Golang : When to use init() function?
+30.4k Golang : How to verify uploaded file is image or allowed file types
+12.1k Golang : Save webcamera frames to video file
+19.9k Golang : Count JSON objects and convert to slice/array
+12.6k Golang : Exit, terminating or aborting a program
+12.5k Golang : HTTP response JSON encoded data