Coming from languages like Python, Ruby, and JS, I am really struggling with Go right now. It feels overly complex, but I am hoping I am just missing something.
Right now I have code that can successfully call Boston's MBTA API (using their public developer key) and return all route information.
I have dropped the code here: http://pastebin.com/PkBaP714 and here: http://pastebin.com/7mRxgrpp
Sample data returned: http://pastebin.com/M2hzMKYs
I want to return two things 1) JUST each route_type and mode_name, and 2) when route_type is called each of the route_id and route_name.
For whatever reason I am just totally lost. I've spent 16 hours staring at documentation and I feel like I am looking at a foreign language :).
It may be too much to ask for specific help, but I would LOVE IT.
Just map them to a new type:
func main() {
flag.Parse()
c := gombta.Client{APIKey: apikey, URL: apiurl}
// get a list of routes by type
d, err := c.GetRoutes(format)
check(err)
var toPrint interface{}
if typeid == 9999 {
type Result struct {
RouteType string `json:"route_type"`
ModeName string `json:"mode_name"`
}
rs := []Result{}
for _, m := range d.Mode {
rs = append(rs, Result{
RouteType: m.RouteType,
ModeName: m.ModeName,
})
}
toPrint = rs
} else {
type Result struct {
RouteID string `json:"route_id"`
RouteName string `json:"route_name"`
}
rs := []Result{}
for _, m := range d.Mode {
if fmt.Sprint(typeid) == m.RouteType {
for _, r := range m.Route {
rs = append(rs, Result{
RouteID: r.RouteID,
RouteName: r.RouteName,
})
}
}
}
toPrint = rs
}
j, err := json.MarshalIndent(toPrint, "", " ")
fmt.Printf("RouteTypes: ")
os.Stdout.Write(j)
}