记使用WaitGroup时的一个错误
记使用WaitGroup时的一个错误 最近重构我之前写的服务器代码时,不当使用了WaitGroup,碰到了个错误,记录下. package main import ( "fmt" "sync" "time" ) func main() { var wg sync .WaitGroup wg .Add ( 1 ) f1 := func() { time .Sleep (time .Second * 2 ) fmt .Println ( "func()" ) wg .Done () } go f1() go f1() go f1() wg .Wait () fmt .Println ( "Done" ) } /* D:\test\>go run testwg2.go func() func() panic: sync: negative WaitGroup counter goroutine 22 [running]: runtime.panic(0x4a56e0, 0xc082000250) c:/go/src/pkg/runtime/panic.c:279 +0x11f sync.(*WaitGroup).Add(0xc0820045e0, 0xffffffffffffffff) c:/go/src/pkg/sync/waitgroup.go:64 +0x9a sync.(