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
+8.9k Golang : Intercept and compare HTTP response code example
+8.6k Golang : Get final balance from bit coin address example
+26k Golang : Convert(cast) string to uint8 type and back to string
+8.8k Golang : Get curl -I or head data from URL example
+12.7k Golang : Get terminal width and height example
+9.9k Golang : Print how to use flag for your application example
+13.3k Golang : Get user input until a command or receive a word to stop
+18.9k Golang : Check if directory exist and create if does not exist
+8.8k Golang : Handle sub domain with Gin
+12.1k Golang : Validate email address
+8.1k Useful methods to access blocked websites
+10.4k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example