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
+21k Golang : Saving private and public key to files
+5.3k Linux : How to set root password in Linux Mint
+4k Java : Random alphabets, alpha-numeric or numbers only string generator
+25.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+13.2k Python : Convert IPv6 address to decimal and back to IPv6
+30.8k Golang : Generate random string
+29.7k Golang : JQuery AJAX post data to server and send data back to client example
+7.4k Golang : Dealing with postal or zip code example
+7.7k Golang : Handling Yes No Quit query input
+11.8k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.7k Golang : Stop goroutine without channel
+11.9k Golang : Calculations using complex numbers example