go run main.go
2021/11/15 11:25:02 openWeatherMap: Bucharest: 280.32
Last active
November 17, 2021 08:42
-
-
Save amuraru/ef3977ce596cf25a5bcadb09cc4187f6 to your computer and use it in GitHub Desktop.
Intro Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
) | |
type openWeatherMap struct{} | |
func (w openWeatherMap) temperature(city string) (float64, error) { | |
apiKey := "appid=542ffd081e67f4512b705f89d2a611b2" | |
resp, err := http.Get("http://samples.openweathermap.org/data/2.5/weather?" + apiKey + "&q=" + city) | |
if err != nil { | |
return 0, err | |
} | |
defer resp.Body.Close() | |
var d struct { | |
Main struct { | |
Kelvin float64 `json:"temp"` | |
} `json:"main"` | |
} | |
if err := json.NewDecoder(resp.Body).Decode(&d); err != nil { | |
return 0, err | |
} | |
log.Printf("openWeatherMap: %s: %.2f", city, d.Main.Kelvin) | |
return d.Main.Kelvin, nil | |
} | |
func main() { | |
w := &openWeatherMap{} | |
w.temperature("Bucharest") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment