for i := 0; i < len(rf.peers); i++ {
DPrintf("i = %d", i)
if i == rf.me {
DPrintf("skipping myself #%d", rf.me)
continue
}
go func() {
DPrintf("len of rf.peers = %d", len(rf.peers))
DPrintf("server #%d sending request vote to server %d", rf.me, i)
reply := &RequestVoteReply{}
ok := rf.sendRequestVote(i, args, reply)
if ok && reply.VoteGranted && reply.Term == rf.currentTerm {
rf.voteCount++
if rf.voteCount > len(rf.peers)/2 {
rf.winElectionCh <- true
}
}
}()
}
for val := range values {
go val.MyMethod()
}
for val := range values {
go func() {
fmt.Println(val)
}()
}
The val variable in the above loops is actually a single variable that takes on the value of each slice element. Because the closures are all only bound to that one variable, there is a very good chance that when you run this code you will see the last element printed for every iteration instead of each value in sequence, because the goroutines will probably not begin executing until after the loop.
for val := range values {
go func(val interface{}) {
fmt.Println(val)
}(val)
}
for i := range valslice {
val := valslice[i]
go func() {
fmt.Println(val)
}()
}
server := i
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有