Golang encoding/json.NewEncoder() function example
package encoding/json
NewEncoder returns a new encoder that writes to w ( 1st parameter )
Golang encoding/json.NewEncoder() 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: 20,
Job: "CEO",
}
encoder := json.NewEncoder(os.Stdout) //output to screen
if err := encoder.Encode(worker); err != nil {
fmt.Println(err)
}
}
Output :
{"Name":"Adam","Age":20,"Job":"CEO"}
Reference :
Advertisement
Something interesting
Tutorials
+7.7k Golang : Command line ticker to show work in progress
+13k Swift : Convert (cast) Int to String ?
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+9.6k Golang : How to extract video or image files from html source code
+9.2k Golang : How to check if a string with spaces in between is numeric?
+6.8k Get Facebook friends working in same company
+36k Golang : Get file last modified date and time
+4.9k Nginx and PageSpeed build from source CentOS example
+37.7k Golang : Comparing date or timestamp
+4.6k Linux : sudo yum updates not working
+21.2k Golang : How to get time zone and load different time zone?
+10.8k PHP : Convert(cast) bigInt to string