watigroup是用来控制一组goroutine的,用来等待一组goroutine结束
比如关于kafka的消费者代码除了生硬的让程序等待一个小时,也可以这样写
package main import (
"sync"
"github.com/Shopify/sarama"
"strings"
"fmt"
) var wg sync.WaitGroup//用来等到一组goroutine结束 func main() {
consumer, err := sarama.NewConsumer(strings.Split("localhost:9092", ","), nil)
if err != nil {
fmt.Println("failed to start consumer:%s", err)
return
}
partitionList, err := consumer.Partitions("nginx_long")
if err != nil {
fmt.Println("failed to get the list of partitions:", err)
}
fmt.Println(partitionList)
for partition := range partitionList{
pc, err := consumer.ConsumePartition("nginx_log", int32(partition),sarama.OffsetNewest)
if err != nil {
fmt.Println("failed to start consumer for partition %d:%s\n", partition, err)
return
}
defer pc.AsyncClose()
go func(sarama.PartitionConsumer) { //kafka有几个分区就起几个goroutine
wg.Add() //
for msg := range pc.Messages(){//返回一个channel
fmt.Printf("consumer Partition:%d, Offset:%d, key:%s, value:%s", msg.Partition, msg.Offset, string(msg.Key), string(msg.Value))
fmt.Println()
}
wg.Done()
}(pc)
}
wg.Wait()
//time.Sleep(time.Hour)
consumer.Close()
}
wg.add()用来加1,wg.done()用来减1,wg.wait()用来确认收到等待信号,程序可以继续执行,对于上面的例子:
1、kafka有几个分区,程序就会启动几个goroutine,在它的匿名函数里,每新起一个goroutine,wg.add()都会被执行
2、每一个for循环结束后,wg.done()也会被执行,当wg.done()等于0时,wg.wait()收到信号,程序执行consumer.close()