刚学go不久,遇到比较奇怪的事情,想请教一下。(另外排版比较挫,见谅)
是这样的
leaderSendAppendEntries里面通过goroutine来执行 rf.sendAppendEntriesRPC(index, &args, &reply),然后发现这么传的话args到了RequestAppendEntiresRPC里面总是nil。
但是通过rf.sendAppendEntriesRPC(index, argsOutsize, &reply)的方式来发送,也就是从goroutine外传输argsOutsize指针,就没什么问题,这个是什么原因呢?
`func (rf *Raft) leaderSendAppendEntries(cmd interface{}){`
argsOutsize := AppendEntriesRPCArgs{...}
go func(rf *Raft, index int, argsOutsize *AppendEntriesRPCArgs, result []bool) {
for {
reply := AppendEntriesRPCReply{}
args := AppendEntriesRPCArgs{
Term: rf.currentTerm,
LeaderId: rf.me,
PrevLogIndex: rf.nextIndex[index] - 1,
PrevLogTerm: rf.log[rf.nextIndex[index]-1].(logContent).Term,
LeaderCommit: rf.commitIndex,
Entries: make([]interface{}, 0),
}
if cmd != nil {
(args).Entries = append(args.Entries, rf.log[args.PrevLogIndex+1:]...)
}
ok := rf.sendAppendEntriesRPC(index, &args, &reply)
//ok := rf.sendAppendEntriesRPC(index,argsOutsize , &reply)
}
}(rf, index, &argsOutsize , result)
}
}
```func (rf *Raft) sendAppendEntriesRPC(server int, args *AppendEntriesRPCArgs, reply *AppendEntriesRPCReply) bool {```
ok := rf.peers[server].Call("Raft.RequestAppendEntiresRPC", args, reply)
return ok
}
有疑问加站长微信联系(非本文作者)