-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_way.go
80 lines (51 loc) · 1.31 KB
/
bad_way.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
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"fmt"
"os"
)
func getUsers() []UserData {
url := "https://gorest.co.in/public/v1/users"
res := getRequest(url)
if res == nil {
return nil
}
var users User
err := json.Unmarshal(res, &users)
failOnError(err, "Error on parsing response")
return users.Data
}
func getUserTodos() []UserTodoData {
url := "https://gorest.co.in/public/v1/todos"
res := getRequest(url)
if res == nil {
return nil
}
var todo Todo
err := json.Unmarshal(res, &todo)
failOnError(err, "Error on parsing response")
return todo.Data
}
func doItBadway() {
defer elapsed("badWay")()
users := getUsers()
todos := getUserTodos()
if users != nil {
userFile, err := os.Create("users.txt")
failOnError(err, "Error on creating users.txt")
defer userFile.Close()
for _, u := range users {
fmt.Printf("Id: %d, Name: %s, Email: %s\n", u.Id, u.Name, u.Email)
fmt.Fprintf(userFile, "Id: %d, Name: %s, Email: %s\n", u.Id, u.Name, u.Email)
}
}
if todos != nil {
todoFile, err := os.Create("todos.txt")
failOnError(err, "Error on creating todos.txt")
defer todoFile.Close()
for _, u := range todos {
fmt.Printf("Id: %d, UserId: %d, Title: %s\n", u.Id, u.UserId, u.Title)
fmt.Fprintf(todoFile, "Id: %d, UserId: %d, Title: %s\n", u.Id, u.UserId, u.Title)
}
}
}