Skip to content

Instantly share code, notes, and snippets.

@ear7h
Created August 27, 2017 08:43
Show Gist options
  • Save ear7h/0c8869cbf3b3362704627077fd1c0826 to your computer and use it in GitHub Desktop.
Save ear7h/0c8869cbf3b3362704627077fd1c0826 to your computer and use it in GitHub Desktop.
learned about goto statements in Go and actually found a use case
func getMarketHours() (time.Time, time.Time) {
url := "https://api.robinhood.com/markets/XNAS/hours/" + time.Now().Format("2006-01-02") + "/"
//tag for goto statement
L:
//get today's market info
res, err := http.Get(url)
if err != nil {
panic(err)
}
byt, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var m map[string]interface{}
err = json.Unmarshal(byt, &m)
if err != nil {
panic(err)
}
//if the market is open on fetched day and the close time is in the future
//then parse time strings
if m["is_open"].(bool) {
openTime, err := time.Parse(time.RFC3339, m["opens_at"].(string))
closeTime, err := time.Parse(time.RFC3339, m["closes_at"].(string))
if err != nil {
panic(err)
}
if closeTime.After(time.Now()) {
return openTime, closeTime
}
}
//else get the next open hours
url= m["next_open_hours"].(string)
//and fetch again
goto L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment