我正在研究http 的源代码Golang,我发现了这个func NewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*Request, error) { ... rc, ok := body.(io.ReadCloser) ...}但这body是nil,它是通过以下方式传递的func NewRequest(method, url string, body io.Reader) (*Request, error) { return NewRequestWithContext(context.Background(), method, url, body)}func (c *Client) Get(url string) (resp *Response, err error) { req, err := NewRequest("GET", url, nil) if err != nil { return nil, err } return c.Do(req)}函数Get传递nil给函数NewRequest,函数NewRequest将 this 传递nil给函数NewRequestWithContext,那么函数 NewRequestWithContext用来nil调用nil.(io.ReadCloser),为什么不引起panic呢?
- 2 回答
- 0 关注
- 69 浏览
添加回答
举报
0/150
提交
取消