Mastering-Go-Second-Edition/ch04/mUJSON.go
2019-02-01 11:31:50 +02:00

42 lines
670 B
Go

package main
import (
"encoding/json"
"fmt"
)
type Record struct {
Name string
Surname string
Tel []Telephone
}
type Telephone struct {
Mobile bool
Number string
}
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"},
}}
rec, err := json.Marshal(&myRecord)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(rec))
var unRec Record
err1 := json.Unmarshal(rec, &unRec)
if err1 != nil {
fmt.Println(err1)
return
}
fmt.Println(unRec)
}