Try 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 will simply be "It will print." Exit goroutines printElo()and printHellowill not be radiated, because, in my opinion, the flow of functions main()completed before goroutines can even start running.
What is the idiomatic way to do similar code work in the Golang and not end prematurely?
source
share