-
Book Overview & Buying
-
Table Of Contents
Practical Systems Programming in Go
By :
JSON has become the standard format for exchanging structured data between systems, and Go provides excellent support for working with it. In this section, you will learn how to process JSON streams without loading everything into memory and how to convert JSON records to the XML format.
In this section, we will process streams of JSON records efficiently. Instead of reading it line by line or loading entire files into memory, we will use the json.Decoder() type, which reads and decodes one JSON object at a time directly from an input stream. Look at the following implementation of main() as found in ch07/json.go, which reads and processes a stream of JSON records from a file, decoding each record as it arrives, making it ideal for logs, event data, or network streams:
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage:", os.Args[0], "<json-stream-file>")
os.Exit(1)
}...