August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn how to write JSON data. The utility that will be presented in three parts is called writeJSON.go and it will write to standard output (os.Stdout), which means that it will write on the terminal screen.
The first part of writeJSON.go is as follows:
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 writeJSON.go is the following:
func saveToJSON(filename *os.File, key interface{}) {
encodeJSON := json.NewEncoder(filename)
err := encodeJSON.Encode(key)
if err != nil {
fmt.Println(err)
return
}
}
The saveToJSON() function does all the work for us as it creates ...
Read now
Unlock full access