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
+5.9k Unix/Linux : Get reboot history or check when was the last reboot date
+20.4k Golang : Determine if directory is empty with os.File.Readdir() function
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+15.8k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+7.4k Golang : Calculate how many weeks left to go in a given year
+38.2k Golang : Read a text file and replace certain words
+4.7k Mac OSX : Get disk partitions' size, type and name
+17.1k Golang : How to save log messages to file?
+5.3k Python : Convert(cast) string to bytes example
+8.8k Golang : Random integer with rand.Seed() within a given range
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+12.9k Golang : http.Get example