FAQ
Hi,

As per the Go Memory Model (http://golang.org/ref/mem), the below code
should print "hello, world" twice. But I am not getting any output.
Can anyone let me know what I am missing?

package main

import "sync"
import "fmt"

var a string
var once sync.Once

func setup() {
a = "hello, world"
}

func doprint() {
once.Do(setup)
fmt.Println(a)
}

func twoprint() {
go doprint()
go doprint()
}

func main() {
twoprint()
}

http://play.golang.org/p/7yt6hXIZM4

Thanks

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

  • David Symonds at Feb 13, 2014 at 12:25 am
    The memory model has nothing to do with that. The issue is that your
    program exits as soon as main finishes, and nothing is waiting for the
    fmt.Println in doprint to run. Stick a "time.Sleep(1*time.Second)" at
    the end of main and you'll see it working as you expect.

    --
    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.
  • Jesse McNelis at Feb 13, 2014 at 12:28 am

    On 13/02/2014 11:24 AM, "mohan" wrote:
    Hi,

    As per the Go Memory Model (http://golang.org/ref/mem), the below code
    should print "hello, world" twice. But I am not getting any output.
    Can anyone let me know what I am missing?
    Your program finished before any of the goroutines had a chance to do
    anything.

    --
    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
postedFeb 13, '14 at 12:24a
activeFeb 13, '14 at 12:28a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase