Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (62 loc) · 1.61 KB
/
Copy pathmain.go
File metadata and controls
67 lines (62 loc) · 1.61 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type Weather struct {
Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
} `json:"coord"`
Weather []struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Base string `json:"base"`
Main struct {
Temp float64 `json:"temp"`
FeelsLike float64 `json:"feels_like"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
} `json:"main"`
Visibility int `json:"visibility"`
Wind struct {
Speed float64 `json:"speed"`
Deg int `json:"deg"`
} `json:"wind"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Dt int `json:"dt"`
Sys struct {
Type int `json:"type"`
ID int `json:"id"`
Country string `json:"country"`
Sunrise int `json:"sunrise"`
Sunset int `json:"sunset"`
} `json:"sys"`
Timezone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
}
func main() {
var weather Weather
response, err := http.Get("https://api.openweathermap.org/data/2.5/weather?lat=46.94&lon=7.40&appid=bb7302fe136e4097821c1f74e3a51765")
if err != nil {
fmt.Print(err.Error())
return
}
if responseData, err := io.ReadAll(response.Body); err != nil {
fmt.Println(err)
} else {
json.Unmarshal(responseData, &weather)
}
fmt.Printf("The current temperature in %s is %.2f°C.\n", weather.Name, weather.Main.Temp-273.15)
}