-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
44 lines (36 loc) · 829 Bytes
/
data.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func fetchCountries() (string, error) {
response, err := http.Get("https://api.covid19api.com/summary")
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err2 := ioutil.ReadAll(response.Body)
if err2 != nil {
panic(err2)
}
return string(body), nil
}
type Data struct {
Countries []struct {
Country string `json:"Country"`
Slug string `json:"Slug"`
} `json:"Countries"`
}
func getCountries() map[int][]string {
jsonString, _ := fetchCountries()
var data Data
if err := json.Unmarshal([]byte(jsonString), &data); err != nil {
panic(err)
}
countriesSlice := map[int][]string{}
for index, element := range data.Countries {
countriesSlice[index] = []string{element.Slug, element.Country}
}
return countriesSlice
}