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
It seems like the referenced doc is misleading:
go/doc/go_mem.html
Line 425 in 66ff373
What did you do?
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