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
+18.5k Golang : How to count the number of repeated characters in a string?
+18.3k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+5k Golang : Shuffle array of list
+4.2k Golang : Pad file extension automagically
+8.6k Golang : Validate IPv6 example
+5.9k Golang : Takes a plural word and makes it singular
+8.4k Golang : Accessing content anonymously with Tor
+17.6k Golang : Get host name or domain name from IP address
+14.9k Golang : Read integer from file into array
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+5k Golang : How to write backslash in string?
+39.7k Golang : How to check if a string contains another sub-string?