I've been playing around with the following code from A Tour of Go, but I don't understand what is going on when I apply some minor changes. The original code is this
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")
}
and it produces this
world
hello
hello
world
world
hello
hello
world
world
hello
which is OK: five times hello, five times world. I starts to get strange when I call
say("world")
go say("hello")
Now the output is just
world
world
world
world
world
No hello whatsoever. It gets even weirder with two goroutines
go say("world")
go say("hello")
Now there is no output at all. When I change i < 5
to i < 2
and call
go say("world")
say("hello")
I get
world
hello
hello
What am I missing here?