Waitgroup.done wont Panic?

58 views
Skip to first unread message

sperber...@googlemail.com

unread,
May 14, 2020, 2:18:56 AM5/14/20
to golang-nuts
Hi Folks,

Here:
https://play.golang.org/p/76AzuAiVGxL

I thougt If the waitgroup gets below 0 there should be a Panic.
I understand that the goroutine is canceled. So maybe the defer Statement is Not executed.
But why is there No warning at the end of Main that some goroutines Had to be canceled?

Thx
Yours
Stephan

Jan Mercl

unread,
May 14, 2020, 2:32:51 AM5/14/20
to sperber...@googlemail.com, golang-nuts
wg.Wait at line 18 does not wait for anything and the program exists
before the goroutine executing func worker even starts. IOW, wg.Done
at line 10 is not executed hence no panic.

Jake Montgomery

unread,
May 14, 2020, 12:17:30 PM5/14/20
to golang-nuts
To clarify what Jan said, a Go program exits when main() exits. Since `wg` has a count of 0, the call to wg.Wait() on like 18 doe not wat, so the program exits before your goroutine even has time to run. With slight modification, it does panic as expected: https://play.golang.org/p/ivcfhsu1N9K
Reply all
Reply to author
Forward
0 new messages