Skip to content

doc/go_mem: "hello, world" will not always be printed twice #33815

@ehsangifani

Description

@ehsangifani

It seems like the referenced doc is misleading:

The result will be that <code>"hello, world"</code> will be printed

What did you do?

package main

import "sync"

var a string
var once sync.Once

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

func doprint() {
	once.Do(setup)
	print(a)
}

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

func main() {
	twoprint()
}

What did you expect to see?

The result will be that "hello, world" will be printed twice.

What did you see instead?

Sometimes it was printed twice, and sometimes it was not printed at all.

System details

go version go1.12.9 windows/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="C:\Users\*****\AppData\Local\go-build"
GOEXE=".exe"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="windows"
GOOS="windows"
GOPATH="C:\Users\******\go"
GOPROXY=""
GORACE=""
GOROOT="c:\go"
GOTMPDIR=""
GOTOOLDIR="c:\go\pkg\tool\windows_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: go version go1.12.9 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions