-
Notifications
You must be signed in to change notification settings - Fork 8
/
goexample.go
67 lines (52 loc) · 1.21 KB
/
goexample.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"api-pull/nhlApi"
"io"
"log"
"os"
"sync"
)
func main() {
rosterFile, err := os.OpenFile("rosters.txt", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("error opening the file rosters.txt: %v", err)
}
defer rosterFile.Close()
wrt := io.MultiWriter(os.Stdout, rosterFile)
log.SetOutput(wrt)
teams, err := nhlApi.GetAllTeams()
if err != nil {
log.Fatalf("error while getting all teams: %v", err)
}
var wg sync.WaitGroup
wg.Add(len(teams))
// unbuffered channel
results := make(chan []nhlApi.Roster)
for _, team := range teams {
go func(team nhlApi.Team) {
roster, err := nhlApi.GetRosters(team.ID)
if err != nil {
log.Fatalf("error getting roster: %v", err)
}
results <- roster
wg.Done()
}(team)
}
go func() {
wg.Wait()
close(results)
}()
display(results)
}
func display(results chan []nhlApi.Roster) {
for r := range results {
for _, ros := range r {
log.Println("----------------------")
log.Printf("ID: %d\n", ros.Person.ID)
log.Printf("Name: %s\n", ros.Person.FullName)
log.Printf("Position: %s\n", ros.Position.Abbreviation)
log.Printf("Jersey: %s\n", ros.JerseyNumber)
log.Println("----------------------")
}
}
}