/* Go program to read data from the Velostar service */ package main import ( "fmt" "flag" "os" "http" "xml" "bytes" ) const ( key string = "XXXXXXXXXXX" // Get your own key at // url string = "http://data.keolis-rennes.com/xml/" maxsize int = 65536 ) type Station struct { Id int Number int State int Latitude float Longitude float Name string Slotsavailable int Bikesavailable int District string Lastupdate string } type Data struct { Station Station } type Answer struct { // Status Data Data } type ApiKR struct { Request string Answer Answer } func main() { if flag.NArg() != 1 { fmt.Printf("Usage: gostar query\n") os.Exit(1) } query := flag.Arg(0) completeurl := url + "?version=1.0&key=" + http.URLEscape(key) + "&cmd=getstation¶m[request]=number¶m[value]=" + http.URLEscape(query) response, _, error := http.Get(completeurl) if error == nil { // TODO: test response.Status? body := make([]byte, maxsize) n, _ := response.Body.Read(body) buffer := bytes.NewBuffer(body[0:n]) result := ApiKR{} error := xml.Unmarshal(buffer, &result) if error == nil { fmt.Printf("Station %s\nBikes available: %d\nSlots available: %d\n", result.Answer.Data.Station.Name, result.Answer.Data.Station.Bikesavailable, result.Answer.Data.Station.Slotsavailable) } else { fmt.Printf("XML error %s\n", error) } } else { fmt.Printf("HTTP error %s\n", error) } }