August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you will learn how to read a JSON record from disk using the code of readJSON.go, which will be presented in three parts.
The first part of readJSON.go is shown in the following Go code:
package main
import (
"encoding/json"
"fmt"
"os"
)
type Record struct {
Name string
Surname string
Tel []Telephone
}
type Telephone struct {
Mobile bool
Number string
}
In this Go code, we define the structure variables that are going to keep the JSON data.
The second part of readJSON.go is as follows:
func loadFromJSON(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 { return err } in.Close() ...Read now
Unlock full access