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
+8k Golang : Capture text return from exec function example
+11.6k Golang : Exit, terminating or aborting a program
+20k Curl usage examples with Golang
+13.1k Elastic Search : Mapping date format and sort by date
+13.7k Golang : ROT47 (Caesar cipher by 47 characters) example
+17.3k Golang : Find IP address from string
+9.1k Golang : Channels and buffered channels examples
+8.8k Golang : Resumable upload to Google Drive(RESTful) example
+5.5k Golang : Map within a map example
+22.9k Golang : Upload to S3 with official aws-sdk-go package
+22k Golang : Randomly pick an item from a slice/array example