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
+9.9k Golang : How to generate Code 39 barcode?
+19.2k Golang : Read input from console line
+17.5k Golang : When to use init() function?
+4k Java : Random alphabets, alpha-numeric or numbers only string generator
+10.5k Golang : Use regular expression to get all upper case or lower case characters example
+7.1k How to let Facebook Login button redirect to a particular URL ?
+89k Golang : How to convert character to ASCII and back
+19.6k Golang : How to count the number of repeated characters in a string?
+41.6k Golang : How to count duplicate items in slice/array?
+11.2k Golang : Create Temporary File
+12.4k Golang : Save webcamera frames to video file
+10.1k Golang : Detect number of active displays and the display's resolution