Created
November 29, 2016 03:51
-
-
Save FrancisBaileyH/e66a9dfab2789d9093b384146fc7f228 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
package main | |
import( | |
"encoding/json" | |
"net/http" | |
"net/url" | |
"fmt" | |
) | |
func main() { | |
query := "select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"kelowna, bc\") and u=\"c\"" | |
var curl *url.URL | |
curl, _ = url.Parse("http://query.yahooapis.com/v1/public/yql") | |
parameters := url.Values{} | |
parameters.Add("q", query) | |
parameters.Add("format", "json") | |
curl.RawQuery = parameters.Encode() | |
response, err := http.Get(curl.String()) | |
if err != nil { | |
panic(err.Error()) | |
} | |
weather := new(WeatherAPIResponse) | |
err = json.NewDecoder(response.Body).Decode(weather) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("\nCurrent Weather in Kelowna, BC\n===================================\n") | |
fmt.Printf("Condition: %s\n", weather.Query.Results.Channel.Item.Condition.Text) | |
fmt.Printf("Temperature: %d\n\n", weather.Query.Results.Channel.Item.Condition.Temp) | |
} | |
type WeatherAPIResponse struct { | |
Query Query `json:"query"` | |
} | |
type Query struct { | |
Count int `json:"count"` | |
Created string `json:"created"` | |
Lang string `json:"lang"` | |
Results Results `json:"results"` | |
} | |
type Results struct { | |
Channel Channel `json:"channel"` | |
} | |
type Channel struct { | |
Item Item `json:"item"` | |
} | |
type Item struct { | |
Condition Condition `json:"condition"` | |
} | |
type Condition struct { | |
Code int `json:"code,string"` | |
Date string `json:"date"` | |
Temp int `json:"temp,string"` | |
Text string `json:"text"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment