小编Vla*_*uer的帖子

Golang http包:Request.WithContext方法src代码说明

以下是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)

或者我会错过什么?

pointers http go

3
推荐指数
1
解决办法
543
查看次数

标签 统计

go ×1

http ×1

pointers ×1