Skip to content

Instantly share code, notes, and snippets.

@hadv
Created September 10, 2017 11:04
Show Gist options
  • Save hadv/72ebbfbc37782f4bd431f76e739c1ed2 to your computer and use it in GitHub Desktop.
Save hadv/72ebbfbc37782f4bd431f76e739c1ed2 to your computer and use it in GitHub Desktop.
type openWeatherMap struct {
apiKey string
}
func (w openWeatherMap) temperature(city string) (float64, error) {
resp, err := http.Get("http://api.openweathermap.org/data/2.5/weather?APPID=" + w.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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment