New to Go, I've implemented a small Ticker
to poll an API at a given interval:
func Poll() <-chan map[uint8]Data {
json := make(chan map[uint8]Data)
user, pass, endpoint := credentials.Get()
ticker := time.NewTicker(90 * time.Second)
client := &http.Client{}
req, _ := http.NewRequest("GET", endpoint, nil)
req.SetBasicAuth(user, pass)
go func() {
for range ticker.C {
resp, _ := client.Do(req)
bodyText, _ := ioutil.ReadAll(resp.Body)
json <- extract(string(bodyText))
}
}()
return json
}
Obviously this waits until the first whole interval has elapsed before calling the API for the first poll; that's not desirable.
This naive (but working) solution seems...weird:
go func() {
resp, _ := client.Do(req)
bodyText, _ := ioutil.ReadAll(resp.Body)
json <- extract(string(bodyText))
}()
go func() {
for range ticker.C {
resp, _ := client.Do(req)
bodyText, _ := ioutil.ReadAll(resp.Body)
json <- extract(string(bodyText))
}
}()
Is there a better or more more idiomatic Go way to accomplish this?
I've done it like this in the past:
for ; true; <-ticker.C {
resp, _ := client.Do(req)
bodyText, _ := ioutil.ReadAll(resp.Body)
json <- extract(string(bodyText))
}
For example:
t := time.NewTicker(2 * time.Second)
now := time.Now()
for ; true; <-t.C {
fmt.Println(time.Since(now))
}
https://play.golang.org/p/1wz99kzZZ92
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With