I'm going through 'A Tour of Go' and have been editing most of the lessons to make sure I fully understand them. I have a question regarding: https://tour.golang.org/concurrency/1
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Leaving main
the way it is produces a random ordering of hellos and worlds because the threads are executing in different orders each time the program runs. I have two questions:
- If I remove
go
from the line with world and add it to the line with hello, world is printed 5 times and hello is not printed at all. Why is that? - If I add
go
in front of both lines, nothing is printed at all. Why is that?
I have some experience with concurrency with C++ (although it was a while ago) and some more recent experience with Python, but would describe my overall experience with concurrency fairly novice-level.
Thanks!