golang 并发执行查询

package main

import (
	"fmt"
	"sync"
)

func main() {

	var l []int
	receive := func(w chan int, wg *sync.WaitGroup) {
		for data := range w {
			l = append(l, data)
			wg.Done()
		}
		close(w)
	}

	handler:= func(index int, w chan int) {
		w <- index
	}

	wg := new(sync.WaitGroup)
	ws := make(chan int)
	go receive(ws, wg)
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go handler(i, ws)
	}
	wg.Wait()
	fmt.Print(len(l), "\t")

}

posted @ 2022-05-31 15:13  vx_guanchaoguo0  阅读(161)  评论(0编辑  收藏  举报