以下是http/request.go第290行源代码的摘录:
// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")
}
r2 := new(Request) //
*r2 = *r // strange gymnastics
r2.ctx = ctx //
return r2
}
Run Code Online (Sandbox Code Playgroud)
我看了一个小时试图理解最后4行.为什么体操?它不代表以下内容:
r.ctx = ctx
return r
Run Code Online (Sandbox Code Playgroud)
或者我会错过什么?