-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
45 lines (40 loc) · 907 Bytes
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
)
type Instructor struct {
Name string `json:"name"`
Email string `json:"email"`
}
type Workshop struct {
Title string `json:"title"`
Instructors []Instructor `json:"instructors"`
}
func main() {
filename := flag.String("file", "", "Input file name")
flag.Parse()
if *filename == "" {
fmt.Fprintln(os.Stderr, "Error: no input file specified")
os.Exit(1)
}
file, err := os.Open(*filename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: cannot open input file")
os.Exit(1)
}
defer file.Close()
var jsonData Workshop
err = json.NewDecoder(file).Decode(&jsonData)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Workshop: %s\n", jsonData.Title)
fmt.Println("Instructors:")
for _, instructor := range jsonData.Instructors {
fmt.Printf(" %s (%s)\n", instructor.Name, instructor.Email)
}
}