Have loook at this contrived example:
package main
import "fmt"
func printElo() {
fmt.Printf("Elo\n")
}
func printHello() {
fmt.Printf("Hello\n")
}
func main() {
fmt.Printf("This will print.")
i := 0
for i < 10 {
go printElo()
go printHello()
i++
}
}
The output of this program would be just "This will print". Output of goroutines printElo()
and printHello
will not be emitted because, I guess, the main()
function thread will finish before the goroutines have a chance to even start executing.
What is the idiomatic way to make similar code work in Golang and not terminate prematurely?