This repository demonstrates how to use streaming JSON encoding in Go to write JSON data directly to an output stream. It showcases encoding a slice of structs into JSON format and optionally formatting the output with indentation for readability.
- This example shows how to use the `json.Encoder` type to stream JSON encoding directly to an output stream, such as standard output.
- It demonstrates how to set indentation for better readability and encode each struct in a slice individually.
package main
import (
"encoding/json"
"fmt"
"os"
)
type Person10 struct {
Name string
Age int
Country string
}
func main() {
people := []Person10{
{"John", 30, "USA"},
{"Alice", 28, "Canada"},
{"Bob", 25, "UK"},
}
// Create a new JSON encoder that writes to standard output
encoder := json.NewEncoder(os.Stdout)
// Optionally set the encoder to use indentation for better readability
encoder.SetIndent("", " ")
// Encode each person individually
for _, person := range people {
err := encoder.Encode(person)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
}
}
-
Make sure you have Go installed. If not, you can download it from here.
-
Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
-
Navigate to the
010_streaming_json_encoding
directory:cd go_sample_examples/030_json/010_streaming_json_encoding
-
Run the Go program:
go run 010_streaming_json_encoding.go
When you run the program, you should see the following output:
{
"Name": "John",
"Age": 30,
"Country": "USA"
}
{
"Name": "Alice",
"Age": 28,
"Country": "Canada"
}
{
"Name": "Bob",
"Age": 25,
"Country": "UK"
}