ryan....@gmail.com writes:
> I'm learning go and trying to figure out the preferred way to call a
Firstly, please use gofmt.
> function every X amount of seconds, similar to setInterval in
> javascript.
>
> Here is a gist of two ways Ive thought of how to accomplish this in
> go. The first polling function uses time.Sleep inside a for loop and
> the other waits to receive a message from time.After. Is there a
> better way to accomplish this and are there any differences at runtime
> between the two functions I created?
>
>
https://gist.github.com/4191392
I'd probably just do this (probably not a great idea to do the async
invocation, so I didn't do it here, but depends on your use case):
package main
import (
"log"
"time"
)
func doSomething(s string) {
log.Printf("doing something: %v", s)
}
func startPolling() {
for _ = range time.Tick(2 * time.Second) {
doSomething("awesome")
}
}
func main() {
go startPolling()
select {}
}
--
dustin