Mastering-Go-Second-Edition/ch04/writeJSON.go
Mihalis Tsoukalos c209f855aa Minor changes
2019-05-20 20:25:23 +03:00

40 lines
652 B
Go

package main
import (
"encoding/json"
"fmt"
"os"
)
type Record struct {
Name string
Surname string
Tel []Telephone
}
type Telephone struct {
Mobile bool
Number string
}
func saveToJSON(filename *os.File, key interface{}) {
encodeJSON := json.NewEncoder(filename)
err := encodeJSON.Encode(key)
if err != nil {
fmt.Println(err)
return
}
}
func main() {
myRecord := Record{
Name: "Mihalis",
Surname: "Tsoukalos",
Tel: []Telephone{Telephone{Mobile: true, Number: "1234-567"},
Telephone{Mobile: true, Number: "1234-abcd"},
Telephone{Mobile: false, Number: "abcc-567"},
},
}
saveToJSON(os.Stdout, myRecord)
}