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
+13.3k Golang : Date and Time formatting
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+22.9k Golang : Test file read write permission example
+5.3k Javascript : Change page title to get viewer attention
+10.3k Golang : Convert file content to Hex
+6.4k PHP : Proper way to get UTF-8 character or string length
+33.9k Golang : Call a function after some delay(time.Sleep and Tick)
+19.6k Golang : Set or Add HTTP Request Headers
+18.4k Golang : Logging with logrus
+8.1k Golang : Variadic function arguments sanity check example
+13.4k Golang : Read from buffered reader until specific number of bytes
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line