Golang encoding/json.Encoder.Encode() function example
package encoding/json
Encode writes the JSON encoding of v (1st parameter) to the stream, followed by a newline character.
Golang encoding/json.Encoder.Encode() 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 { // <<---- here
fmt.Println(err)
}
}
Output :
{"Name":"Adam","Age":20,"Job":"CEO"}
Reference :
Advertisement
Something interesting
Tutorials
+10k Golang : Channels and buffered channels examples
+16.3k Golang : convert string or integer to big.Int type
+15.6k Golang : How to convert(cast) IP address to string?
+25.4k Golang : Generate MD5 checksum of a file
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+5.4k Golang : What is StructTag and how to get StructTag's value?
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+7.6k Javascript : Push notifications to browser with Push.js
+14.2k Elastic Search : Mapping date format and sort by date
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name