I'm very new to go and I'm not sure why this code has this output. I understand that sleep will cause the new goroutine to start the other thread for the specified amount of time. I'm trying to sequentially map out the logic and it looks like "world" should always print before "hello".
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(1 * time.Millisecond)
fmt.Println(s, i)
}
}
func main() {
go say("world")
say("hello")
}
Actual Output:
world 0
hello 0
hello 1
world 1
world 2
hello 2
hello 3
world 3
world 4
hello 4
Expected Output:
world 0
hello 0
world 1
hello 1
world 2
hello 2
...etc