Created
September 10, 2017 11:04
-
-
Save hadv/72ebbfbc37782f4bd431f76e739c1ed2 to your computer and use it in GitHub Desktop.
This file contains 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
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