Mastering-Go-Second-Edition/ch04/writeJSON.go
Mihalis Tsoukalos 7d82891e51 Write to os.Stdout
2019-01-31 20:41:12 +02:00

39 lines
649 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)
}