Skip to content

Instantly share code, notes, and snippets.

@noynaert
Last active April 28, 2016 19:39
Show Gist options
  • Save noynaert/259e82569264ff9dafe4015b338e5988 to your computer and use it in GitHub Desktop.
Save noynaert/259e82569264ff9dafe4015b338e5988 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/url"
"log"
"net/http"
"io/ioutil"
"encoding/xml"
)
type reportType struct{
TermsofService string `xml:"response.termsofService"`
}
func main() {
fmt.Println("data is from WeatherUnderground.")
fmt.Println("https://www.wunderground.com/")
var state, city string
state="MO"
city="Saint_Joseph"
baseURL := "http://api.wunderground.com/api/";
apiKey := "734951781f4140d6"
var query string
//set up the query
query = baseURL+apiKey +
"/conditions/q/"+
url.QueryEscape(state)+ "/"+
url.QueryEscape(city)+ ".xml"
fmt.Println("The escaped query: "+query)
//do the query
response, err := http.Get(query)
doErr(err, "After the Query")
var body []byte;
body, err = ioutil.ReadAll(response.Body)
doErr(err, "After the ReadAll")
fmt.Println(body)
fmt.Printf("%s\n",body)
//Pull the data out of the xml
var report reportType
xml.Unmarshal(body,&report)
fmt.Printf("The report: %v\n",report)
}
func doErr( err error, message string){
if err != nil{
log.Panicf("ERROR: %s %s \n", message, err.Error())
}
}
/*
<response>
<version>0.1</version>
<termsofService>
http://www.wunderground.com/weather/api/d/terms.html
</termsofService>
<features></features>
<error>
<type>keynotfound</type>
<description>this key does not exist</description>
</error>
</response>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment