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:
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?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!
The program terminates before you get a chance to see the results.
You can fix this by adding a statement that ensures main
doesn't exit before the other routines are finished.
From Golang - Concurrency:
With a goroutine we return immediately to the next line and don't wait for the function to complete.
They give a code example:
package main
import "fmt"
func f(n int) {
for i := 0; i < 10; i++ {
fmt.Println(n, ":", i)
}
}
func main() {
go f(0)
var input string
fmt.Scanln(&input)
}
In regards to the code above:
This is why the call to the Scanln function has been included; without it the program would exit before being given the opportunity to print all the numbers.