从戴夫·切尼博客,下面的代码显然会导致比赛的情况下仅通过改变来解决func (RPC) version() int到func (*RPC) version() int:
package main
import (
"fmt"
"time"
)
type RPC struct {
result int
done chan struct{}
}
func (rpc *RPC) compute() {
time.Sleep(time.Second) // strenuous computation intensifies
rpc.result = 42
close(rpc.done)
}
func (RPC) version() int {
return 1 // never going to need to change this
}
func main() {
rpc := &RPC{done: make(chan struct{})}
go rpc.compute() // kick off computation in the background
version := …Run Code Online (Sandbox Code Playgroud)