您可以使用通道来实现超时模式:
import "time"
c := make(chan error, 1)
go func() { c <- client.Call("Service", args, &result) } ()
select {
case err := <-c:
// use err and result
case <-time.After(timeoutNanoseconds):
// call timed out
}
Run Code Online (Sandbox Code Playgroud)
该select会阻塞,直到client.Call返回或timeoutNanoseconds经过.