go 关闭chan 广播信号

package main

import (
	"fmt"
	"sync"
)

func hello(num ...int) {
	num[0] = 18
}

func main() {
	closeChan := make(chan struct{})
	wg := &sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int) {
			defer func() {
				wg.Done()
			}()
			<-closeChan
			fmt.Println(i, "closed")
		}(i)
	}

	close(closeChan) //广播关闭
	wg.Wait()
}

  

posted @ 2023-02-22 10:15  fly不起来啊!  阅读(11)  评论(0编辑  收藏  举报