Hi,
I don't believe the semaphore has
anything to do with the wait group counter. So I don't believe it is
relevant to this question. Note that a goroutine can be interrupted at
any time anyways, so whether or not that code is there doesn't matter
for correctness.
At that point, the issue comes
down to "if you call Go and Wait concurrently, may Wait return before
the function passed to Go finishes" and the answer to that is yes. That
is analogous to calling wg.Add and wg.Wait concurrently for a
sync.WaitGroup and it is valid for that to execute as if the Wait has
been called, returned and then Add is called.
The way to avoid that is to only call Wait after all the Go calls have returned. That is, to not call Wait and Go concurrently.