You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
doc/go_mem: "hello, world" will not always be printed twice #33815
Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.
Yes, of course. But the problem is with the documentation that states printing twice happens without any mention of a synchronization method.
Here is my proposed change to the example code that shows the functionality of Once.Do while being correct:
var a string
var once sync.Once
func setup() {
a += "hello, world" //change: to show that it doesn't run twice
}
func doprint(done chan int) {
once.Do(setup)
print(a)
done <- 1
}
func twoprint() {
var done = make(chan int) //change: synchronization
go doprint(done)
go doprint(done)
<-done
<-done
}
On the one hand, the missing func main() does leave out an interesting detail.
On the other hand, the document is full of interesting details already — and adding that detail to one example implies that we would need to add it to the other examples, obscuring the more important point about the specific synchronization mechanism that is the focus of the example.
Is there a way to make the examples more correct without making them substantially more verbose?
changed the title [-]doc: go memory model, "hello, world" will not always be printed twice.[/-][+]doc/go_mem: "hello, world" will not always be printed twice[/+]on Aug 26, 2019
@bcmills
Actually, I didn't assume anything. As you can see on this line , it says calling twoprint will call setup exactly once.
So it's the issue of wording in my opinion, which could create a wrong impression to someone new to Go, that sync.Once somehow stops main from returning before its spawned go-routines return.
I think changing the sentence to something like: calling twoprint , assuming main() won't return yet, will call setup exactly once.
can somewhat mitigate the situtation.
Activity
DisposaBoy commentedon Aug 24, 2019
You need some kind of synchronization like a channel or
sync.WaitGroupto wait for the goroutines to complete before returning frommain().Quoting https://golang.org/ref/spec#Program_execution
ehsangifani commentedon Aug 24, 2019
Yes, of course. But the problem is with the documentation that states printing twice happens without any mention of a synchronization method.
Here is my proposed change to the example code that shows the functionality of
Once.Dowhile being correct:bcmills commentedon Aug 26, 2019
You assumed a particular
func main(). This one is guaranteed to always execute both prints — just not guaranteed to terminate!bcmills commentedon Aug 26, 2019
On the one hand, the missing
func main()does leave out an interesting detail.On the other hand, the document is full of interesting details already — and adding that detail to one example implies that we would need to add it to the other examples, obscuring the more important point about the specific synchronization mechanism that is the focus of the example.
Is there a way to make the examples more correct without making them substantially more verbose?
bcmills commentedon Aug 26, 2019
CC @dvyukov @ianlancetaylor
See previously #27808.
[-]doc: go memory model, "hello, world" will not always be printed twice.[/-][+]doc/go_mem: "hello, world" will not always be printed twice[/+]ehsangifani commentedon Sep 5, 2019
@bcmills
Actually, I didn't assume anything. As you can see on this line , it says calling
twoprintwill callsetupexactly once.So it's the issue of wording in my opinion, which could create a wrong impression to someone new to Go, that
sync.Oncesomehow stopsmainfrom returning before its spawned go-routines return.I think changing the sentence to something like:
calling
twoprint, assuming main() won't return yet, will callsetupexactly once.can somewhat mitigate the situtation.
seankhliao commentedon Dec 3, 2024
it seems to be an unnecessary detail.