-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.go
95 lines (75 loc) · 1.35 KB
/
currency.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
type GBP struct {
GBP float64 `json:"GBP"`
}
type CurrencyData struct {
Data GBP `json:"data"`
}
type Coins struct {
C500 int
C100 int
C50 int
C10 int
C5 int
C1 int
}
type Notes struct {
N10000 int
N5000 int
N1000 int
}
func main() {
key := os.Getenv("FREE_CURRENCY_API_KEY")
urlTemplate := "https://api.freecurrencyapi.com/v1/latest?apikey=%s¤cies=GBP&base_currency=JPY"
coins := Coins {
C500: 500,
C100: 100,
C50: 50,
C10: 10,
C5: 5,
C1: 1,
}
notes := Notes {
N10000: 10000,
N5000: 5000,
N1000: 1000,
}
wallet := [9]int {
notes.N10000 * 5,
notes.N5000 * 1,
notes.N1000 * 5,
coins.C500 * 1,
coins.C100 * 4,
coins.C50 * 5,
coins.C10 * 8,
coins.C5 * 7,
coins.C1 * 14,
}
if (key == "") {
log.Fatalln("Please set you Free Currency API Key")
}
yen := 0
for _, num := range wallet {
yen += num
}
url := fmt.Sprintf(urlTemplate, key)
res, err := http.Get(url);
if err != nil {
log.Fatalln("Couldn't get request");
}
defer res.Body.Close()
var jsonResponse CurrencyData
if err := json.NewDecoder(res.Body).Decode(&jsonResponse); err != nil {
log.Println(err)
log.Fatalln("Can not convert to JSON")
}
gbp := float64(yen) * jsonResponse.Data.GBP;
fmt.Printf("You have ¥%d , which is £%.2f \n", yen , gbp)
}