Skip to content

Commit fd0d6bb

Browse files
committed
fix goroutine loop variable reference issue
1 parent 4aac549 commit fd0d6bb

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

gee-rpc/day6-load-balance/xclient/xclient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
8585
ctx, cancel := context.WithCancel(ctx)
8686
for _, rpcAddr := range servers {
8787
wg.Add(1)
88-
go func() {
88+
go func(rpcAddr string) {
8989
defer wg.Done()
9090
var clonedReply interface{}
9191
if reply != nil {
@@ -102,7 +102,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
102102
replyDone = true
103103
}
104104
mu.Unlock()
105-
}()
105+
}(rpcAddr)
106106
}
107107
wg.Wait()
108108
return e

gee-rpc/day7-registry/xclient/xclient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
8585
ctx, cancel := context.WithCancel(ctx)
8686
for _, rpcAddr := range servers {
8787
wg.Add(1)
88-
go func() {
88+
go func(rpcAddr string) {
8989
defer wg.Done()
9090
var clonedReply interface{}
9191
if reply != nil {
@@ -102,7 +102,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
102102
replyDone = true
103103
}
104104
mu.Unlock()
105-
}()
105+
}(rpcAddr)
106106
}
107107
wg.Wait()
108108
return e

gee-rpc/doc/geerpc-day6.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
259259
ctx, cancel := context.WithCancel(ctx)
260260
for _, rpcAddr := range servers {
261261
wg.Add(1)
262-
go func() {
262+
go func(rpcAddr string) {
263263
defer wg.Done()
264264
var clonedReply interface{}
265265
if reply != nil {
@@ -276,7 +276,7 @@ func (xc *XClient) Broadcast(ctx context.Context, serviceMethod string, args, re
276276
replyDone = true
277277
}
278278
mu.Unlock()
279-
}()
279+
}(rpcAddr)
280280
}
281281
wg.Wait()
282282
return e

0 commit comments

Comments
 (0)