errgroup: use consistent read for SetLimit panic

This could produce a confusing panic message if the second len(g.sem)
call returns 0. Avoid that by reading it once.

Change-Id: Ibdb4963f90921bc20427b3f1e2de410638f6cb6b
Reviewed-on: https://go-review.googlesource.com/c/sync/+/726280
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/errgroup/errgroup.go b/errgroup/errgroup.go
index 2f45dbc..f69fd75 100644
--- a/errgroup/errgroup.go
+++ b/errgroup/errgroup.go
@@ -144,8 +144,8 @@
 		g.sem = nil
 		return
 	}
-	if len(g.sem) != 0 {
-		panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
+	if active := len(g.sem); active != 0 {
+		panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", active))
 	}
 	g.sem = make(chan token, n)
 }