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
+15.2k Golang : Get timezone offset from date or timestamp
+18k Golang : How to log each HTTP request to your web server?
+8.6k Golang : Progress bar with ∎ character
+9.9k Golang : Sort and reverse sort a slice of integers
+13.9k Golang : Get current time
+14.6k Golang : How to get URL port?
+5.6k Swift : Get substring with rangeOfString() function example
+17.6k Golang : delete and modify XML file content
+20.6k Nginx + FastCGI + Go Setup.
+7.6k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+11.1k Golang : Read until certain character to break for loop
+14.6k Golang : Missing Bazaar command