Help with WaitGroup...https://play.golang.org/p/HvHQbUTT6GJ

go foo() doesn’t say must run after bar() but rather, starting another process to run foo(). That starting has its own meta-processes to initialize before it can run the actual foo(). That’s why foo() comes in later comparing to bar() which is an established process.

Also, keep in mind that by delegating process using goroutine does not guarantees it will start immediately. That’s why foo() somehow looks like it starts later yet I can’t tell how long it will start executing foo().

The execution is random as in there is no telling what (bar or foo) is running at one time on that single cpu so the output is chaotic. Notice the difference between the playground output and the one I quoted. The output are not consistent.

As for why, you can refer to this thread: Question on concurrent.

1 Like