Files
learngo/x-tba/wizards-structs/unmarshal/main.go

36 lines
631 B
Go
Raw Normal View History

2019-04-10 21:00:05 +03:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
2019-04-17 23:28:50 +03:00
// Wizard is one of the greatest of people
type Wizard struct {
// name won't be marshalled (should be exported)
Name string `json:name`
Lastname string `json:"-"`
Nick string `json:"nick"`
}
2019-04-10 21:00:05 +03:00
func main() {
file, err := ioutil.ReadFile("../marshal/wizards.json")
if err != nil {
panic(err)
}
2019-04-17 23:28:50 +03:00
wizards := make([]Wizard, 10)
2019-04-10 21:00:05 +03:00
if json.Unmarshal(file, &wizards) != nil {
panic(err)
}
fmt.Printf("%-15s %-15s\n%s",
"Name", "Nick", strings.Repeat("=", 25))
for _, w := range wizards {
fmt.Printf("%-15s %-15s\n", w.Name, w.Nick)
}
}