FAQ
Hi, all

   here is time.AfterFunc() prototype:

   func AfterFunc(d Duration <http://golang.org/pkg/time/#Duration>, f
func()) *Timer <http://golang.org/pkg/time/#Timer>

   the problem is, AfterFunc doesn't like settimer() of other language, the
callback function f() doesn't has any argumnet.

   when I user AfterFunc create a timer, and the timer arrives, the f() is
called. If I want do something related to my data in function f(), how can
i get it?



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Chris dollin at Sep 22, 2013 at 9:24 am
    On 22 September 2013 10:10, wrote:

    Hi, all
    here is time.AfterFunc() prototype:

    func AfterFunc(d Duration <http://golang.org/pkg/time/#Duration>, f
    func()) *Timer <http://golang.org/pkg/time/#Timer>

    the problem is, AfterFunc doesn't like settimer() of other language, the
    callback function f() doesn't has any argumnet.

    when I user AfterFunc create a timer, and the timer arrives, the f() is
    called. If I want do something related to my data in function f(), how can
    i get it?

    Use a closure.
    timer := AfterFunc(myDuration, func() {... do things here ... } )

    Inside the {} of the closure body, can refer to your data just the
    same as in the surrounding code.

    Chris

    [Note: may cause allocation.]

    --
    Chris "allusive" Dollin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ibrahim M. Ghazal at Sep 22, 2013 at 9:25 am
    Use a closure:

    time.AfterFunc(d, func() {
    doSomething(data)
    })

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 22, '13 at 9:10a
activeSep 22, '13 at 9:25a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase