FAQ
Hullo All,

Consider a short go program:

package main
import (
   "fmt"
   "time"
)
func neverReturn() string {
   time.Sleep(time.Second*5)
   return "DONE"
}
func main() {
   go fmt.Println(neverReturn())
}

(for your convenience: http://play.golang.org/p/W11Oheawxj)

I would expect this program to return immediately, but when I run it on my
computer (`go run example.go`) it actually waits for 5 seconds. This led me
to believe that all arguments must be resolved prior to dispatching the go
routine. However, in the LINK above, which contains the same code, I am
surprised to see that it returns immediately - what I originally thought it
should be doing!

So why the disparity?

-JB

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

  • Dan Kortschak at Jan 24, 2014 at 1:12 am

    On Thu, 2014-01-23 at 11:47 -0800, jfberryman@gmail.com wrote:
    However, in the LINK above, which contains the same code, I am
    surprised to see that it returns immediately - what I originally
    thought it should be doing!

    So why the disparity?
    Time is magic in the playground.

    http://blog.golang.org/playground

    --
    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.
  • Francesc Campoy Flores at Jan 24, 2014 at 2:11 am
    To evaluate the parameters to the Println function neverReturn is called.

    If you want to defer the evaluation of the parameters of Println to the
    execution of a different go routine (therefore not waiting) you should do:

    go func() {
        fmt.Println(neverReturn());
    }

    http://play.golang.org/p/qD-TPt-hAU

    On Thu, Jan 23, 2014 at 5:12 PM, Dan Kortschak wrote:
    On Thu, 2014-01-23 at 11:47 -0800, jfberryman@gmail.com wrote:
    However, in the LINK above, which contains the same code, I am
    surprised to see that it returns immediately - what I originally
    thought it should be doing!

    So why the disparity?
    Time is magic in the playground.

    http://blog.golang.org/playground

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


    --
    --
    Francesc Campoy
    http://campoy.cat

    --
    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.
  • John Berryman at Jan 24, 2014 at 2:49 am
    That all makes sense then. Every argument must be resolved before a go
    process is initiated - and time in the playground is magic. Now everything
    is consistent. Thanks guys.

    John Berryman

    *@JnBrymn <http://bit.ly/YFO5Hs>*
    *LinkedIn* <http://linkd.in/YKGnc8>
    *Blog <http://bit.ly/JohnBlogs>*


    On Thu, Jan 23, 2014 at 9:11 PM, Francesc Campoy Flores
    wrote:
    To evaluate the parameters to the Println function neverReturn is called.

    If you want to defer the evaluation of the parameters of Println to the
    execution of a different go routine (therefore not waiting) you should do:

    go func() {
    fmt.Println(neverReturn());
    }

    http://play.golang.org/p/qD-TPt-hAU


    On Thu, Jan 23, 2014 at 5:12 PM, Dan Kortschak <
    dan.kortschak@adelaide.edu.au> wrote:
    On Thu, 2014-01-23 at 11:47 -0800, jfberryman@gmail.com wrote:
    However, in the LINK above, which contains the same code, I am
    surprised to see that it returns immediately - what I originally
    thought it should be doing!

    So why the disparity?
    Time is magic in the playground.

    http://blog.golang.org/playground

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


    --
    --
    Francesc Campoy
    http://campoy.cat
    --
    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
postedJan 24, '14 at 12:57a
activeJan 24, '14 at 2:49a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase