Skip to content

Instantly share code, notes, and snippets.

@torjusb
Created February 20, 2015 18:10
Show Gist options
  • Save torjusb/9f1cc2e6e5cc9b477118 to your computer and use it in GitHub Desktop.
Save torjusb/9f1cc2e6e5cc9b477118 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type StopVisits struct {
MonitoredStopVisit map[int]MonitoredStopVisit
}
type MonitoredStopVisit struct {
MonitoringRef string
RecordedAtTime string
MonitoredVehicleJourney MonitoredVehicleJourney
}
type MonitoredVehicleJourney struct {
MonitoredCall MonitoredCall
}
type MonitoredCall struct {
AimedArrivalTime string
AimedDepartureTime string
ExpectedArrivalTime string
ExpectedDepartureTime string
VehicleAtStop bool
}
func main() {
resp, _ := http.Get("http://reisapi.ruter.no/stopvisit/getdepartures/3011484")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
stops := make([]MonitoredStopVisit, 0)
json.Unmarshal(body, &stops)
fmt.Printf("%#v", stops[0].MonitoredVehicleJourney.MonitoredCall.ExpectedArrivalTime)
}
@michaelenger
Copy link

What is the benefit of initialising an empty slice rather than just declaring it (line 38)?
Wouldn't this work too?
var stops [] MonitoredStopVisit

@torjusb
Copy link
Author

torjusb commented Feb 20, 2015

Probably none. There is some information on it here: http://stackoverflow.com/questions/25543520/golang-declare-slice-or-make-slice

So I guess var stops [] MonitoredStopVisit would be preferable, as it doesn't allocate any memory until it actually gets data? But I guess in reality, it doesn't really matter (at least in this case) as it will always be populated with data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment