Re: call a function every 30 seconds?

6,031 views
Skip to first unread message

Dustin Sallings

unread,
Dec 2, 2012, 6:33:31 PM12/2/12
to golan...@googlegroups.com
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

Carlos Castillo

unread,
Dec 2, 2012, 6:46:24 PM12/2/12
to golan...@googlegroups.com, ryan....@gmail.com
In the code you have provided there is no real difference semantically between your two examples, except possibly the allocation of an extra channel in the time.After() version (which may need to be garbage collected at some point).

Both examples are very likely to introduce drift, so if that is a concern, you would need another approach. For your situation (a repeating timer), I suggest you use time.Tick/time.NewTicker, as it only needs to be created once and seems to do what you need. Also, with time.Tick you can use it in a for-range loop: http://play.golang.org/p/Ctg3_AQisl

Note: the code doesn't work on play.golang.org, since many features of the time package are disabled for security reasons.

On Sunday, December 2, 2012 3:04:06 PM UTC-8, ryan....@gmail.com wrote:
I'm learning go and trying to figure out the preferred way to call a function every X amount of seconds, similar to setInterval in javascript.

Mark McGranaghan

unread,
Dec 2, 2012, 7:30:28 PM12/2/12
to golan...@googlegroups.com
To extend the examples given by Dustin and Carlos, here's a variant where the ticker is bound to a variable so that it can later by Stop()'d: https://gobyexample.com/tickers

roger peppe

unread,
Dec 4, 2012, 7:24:24 AM12/4/12
to Ryan Fitzgerald, golang-nuts
I often wouldn't bother with time.Tick. This would work fine for many purposes:
http://play.golang.org/p/yxs7HOePwO


On 3 December 2012 00:06, Ryan Fitzgerald <ryan....@gmail.com> wrote:
> Thanks Carlos and Dustin, time.Tick looks like its exactly what Id want to
> use.
> --
>
>

Dustin Sallings

unread,
Dec 4, 2012, 2:47:58 PM12/4/12
to golan...@googlegroups.com
roger peppe <rogp...@gmail.com> writes:

> I often wouldn't bother with time.Tick. This would work fine for many purposes:
> http://play.golang.org/p/yxs7HOePwO

Possibly, but that does something quite a bit different. time.Tick
will stay on schedule and drop invocations when they take too long.

It's the difference between "run every 30 seconds" and "run
continuously with at least 30s between each invocation."

The former is a bit less code and keeps time quite well in my
experiments.

--
dustin

Reply all
Reply to author
Forward
0 new messages