-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.go
52 lines (42 loc) · 802 Bytes
/
weather.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
package main
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
)
type Location struct {
Name string `json:"name"`
Country string `json:"country"`
}
type Condition struct {
Text string `json:"text"`
}
type Current struct {
TempC float64 `json:"temp_c"`
Condition `json:"condition"`
}
type Weather struct {
Location `json:"location"`
Current `json:"current"`
}
func printWeather(res *http.Response) {
body, err := io.ReadAll(res.Body)
if err != nil {
slog.Error(err.Error())
}
var weather Weather
err = json.Unmarshal(body, &weather)
if err != nil {
slog.Error(err.Error())
}
location, current := weather.Location, weather.Current
fmt.Printf(
"%s, %s: %.0fC, %s\n",
location.Name,
location.Country,
current.TempC,
current.Condition.Text,
)
}