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
+47.8k Golang : Convert int to byte array([]byte)
+14.4k Golang : How to convert a number to words
+26.7k Golang : How to check if a connection to database is still alive ?
+7.9k Golang : Ways to recover memory during run time.
+6.5k Golang : Convert an executable file into []byte example
+5.6k Python : Print unicode escape characters and string
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+5k Google : Block or disable caching of your website content
+9k Golang : Inject/embed Javascript before sending out to browser example
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+30.5k Get client IP Address in Go