-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
47 lines (41 loc) · 860 Bytes
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"text/template"
)
type Users []struct {
ID int `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
}
func main() {
APIURL := "https://jsonplaceholder.typicode.com/users"
req, err := http.NewRequest(http.MethodGet, APIURL, nil)
if err != nil {
panic(err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var u Users
json.Unmarshal(body, &u)
msgText := "To: {{.Email}}\nHi {{.Name}}! There is a new post!\n\n\n"
t := template.Must(template.New("msg").Parse(msgText))
for _, r := range u {
err := t.Execute(os.Stdout, r)
if err != nil {
panic(err)
}
}
}