-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatFact.go
48 lines (38 loc) · 808 Bytes
/
catFact.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type catFact struct {
Facts []string
}
func getCatFact() string {
resp, _ := http.Get("http://catfacts-api.appspot.com/api/facts")
body, _ := ioutil.ReadAll(resp.Body)
bodyFix := strings.Replace(string(body), "facts", "Facts", -1)
return bodyFix
}
func parseIntoObject(fact string) catFact {
factBytes := []byte(fact)
var theFact catFact
json.Unmarshal(factBytes, &theFact)
return theFact
}
func outputCatFact(fact catFact) {
fmt.Println("")
fmt.Println(fact.Facts[0])
fmt.Println("")
}
func main() {
count := flag.Int("count", 1, "Number of catfacts to get")
flag.Parse()
for i := 0; i < *count; i++ {
catFact := getCatFact()
fact := parseIntoObject(catFact)
outputCatFact(fact)
}
}