代码示例:(标识:652513c6d90e0)
package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func hello() {
	fmt.Println("hello")
	defer wg.Done()//把计算器-1
}

func main() {
	wg.Add(1)//把计数器+1
	go hello()
	fmt.Println("欢迎来到搜寻编程教程")
	wg.Wait()//阻塞代码的运行,直到计算器为0
}
运行结果: