-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (50 loc) · 996 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"time"
)
type Weather struct {
Water int `json:"water"`
Wind int `json:"wind"`
}
func (w *Weather) checkStatus() (resWater string, resWind string) {
switch {
case w.Water < 5:
resWater = "aman"
case w.Water >= 6 && w.Water <= 8:
resWater = "siaga"
default:
resWater = "tidak terdefinisi"
}
switch {
case w.Wind < 5:
resWind = "aman"
case w.Wind >= 6 && w.Wind <= 8:
resWind = "siaga"
default:
resWind = "tidak terdefinisi"
}
return resWater, resWind
}
func generateJSON(weather Weather) {
res, err := json.Marshal(weather)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("%s\n", res)
resWater, resWind := weather.checkStatus()
fmt.Printf("status air : %s\n", resWater)
fmt.Printf("status angin : %s\n", resWind)
}
func main() {
weather := Weather{}
for {
time.Sleep(2 * time.Second)
weather.Water = rand.Intn(15)
weather.Wind = rand.Intn(15)
generateJSON(weather)
}
}