FAQ
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.

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

--

Search Discussions

  • Dustin Sallings at Dec 2, 2012 at 11:33 pm

    ryan.fitz1@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 at Dec 2, 2012 at 11:46 pm
    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.

    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
    --
  • Ryan Fitzgerald at Dec 3, 2012 at 12:39 am
    Thanks Carlos and Dustin, time.Tick looks like its exactly what Id want to
    use.
    On Sunday, December 2, 2012 6:46:24 PM UTC-5, Carlos Castillo wrote:

    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.

    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
    --
  • Roger peppe at Dec 4, 2012 at 12:24 pm
    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 wrote:
    Thanks Carlos and Dustin, time.Tick looks like its exactly what Id want to
    use.

    On Sunday, December 2, 2012 6:46:24 PM UTC-5, Carlos Castillo wrote:

    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.

    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
    --
    --
  • Dustin Sallings at Dec 4, 2012 at 7:48 pm

    roger peppe 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

    --
  • Mark McGranaghan at Dec 3, 2012 at 12:37 am
    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

    On Monday, December 3, 2012 9:04:06 AM UTC+10, Ryan Fitzgerald 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.

    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
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 2, '12 at 11:12p
activeDec 4, '12 at 7:48p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2017 Grokbase