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
+8.6k Golang : Set or add headers for many or different handlers
+7.9k Golang : Grayscale Image
+6.8k Swift : substringWithRange() function example
+11.3k Golang : Characters limiter example
+31.7k Golang : How to convert(cast) string to IP address?
+6.3k Javascript : Generate random key with specific length
+80.6k Golang : How to return HTTP status code?
+33.8k Golang : convert(cast) bytes to string
+16k Golang : Read large file with bufio.Scanner cause token too long error
+9.2k Golang : Generate Codabar
+9.8k Golang : Qt get screen resolution and display on center example
+5.1k Swift : Convert (cast) Float to Int or Int32 value