September 2017
Intermediate to advanced
466 pages
9h 33m
English
This subsection will illustrate how to read a JSON record and convert it into one Go variable that you can use in your own programs. The name of the presented program will be readJSON.go and will be shown to you in four parts.
The first part of the utility is identical to the first part of the writeJSON.go utility:
package main
import (
"encoding/json"
"fmt"
"os"
)
type Record struct {
Name string
Surname string
Tel []Telephone
}
type Telephone struct {
Mobile bool
Number string
}
The second part of the Go code is the following:
funcloadFromJSON(filename string, key interface{}) error { in, err := os.Open(filename) if err != nil { return err } decodeJSON := json.NewDecoder(in) err = decodeJSON.Decode(key) if err != nil ...Read now
Unlock full access