WaitGroup
In node.js, the process exits when there is no scheduled work. But in go, your program exits when main function exits.
In previouse example, there is a time.Sleep() at the end of the main
function. Without it, the main function will exit immediately and the
goroutines won't be executed.
WaitGroup can be used to wait for multiple goroutines to finish.
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
fmt.Print(1)
wg.Done()
}()
go func() {
fmt.Print(2)
wg.Done()
}()
fmt.Print(3)
wg.Wait()
}