http.Transport
结构的定义可以在源代码https://golang.org/src/net/http/transport.go中找到,如下所示:
type Transport struct { idleMu sync.Mutex wantIdle bool idleConn map[connectMethodKey][]*persistConn idleConnCh map[connectMethodKey]chan *persistConn idleLRU connLRU reqMu sync.Mutex reqCanceler map[*Request]func(error) altMu sync.Mutex altProto atomic.Value Proxy func(*Request) (*url.URL, error) DialContext func(ctx context.Context, network, addr string) (net.Conn, error) Dial func(network, addr string) (net.Conn, error) DialTLS func(network, addr string) (net.Conn, error) TLSClientConfig *tls.Config TLSHandshakeTimeout time.Duration DisableKeepAlives bool DisableCompression bool MaxIdleConns int MaxIdleConnsPerHost int IdleConnTimeout time.Duration ResponseHeaderTimeout time.Duration ExpectContinueTimeout time.Duration TLSNextProto map[string]func(authority string, c *tls.Conn) RoundTripper ProxyConnectHeader Header MaxResponseHeaderBytes int64 nextProtoOnce sync.Once h2transport *http2Transport }
如您所见,http.Transport
是一个包含大量字段的复杂结构。好消息是在编写HTTP相关程序时,并不需要经常使用http.Transport
结构,并且在使用时不需要处理它的所有字段。
http.Transport
结构实现了http.RoundTripper
接口,并且支持HTTP、HTTPS和HTTP代理的模式。不过http.Transport
是一个低级别的结构,本章中使用的http.Client
结构则是一个高级别的HTTP客户端实现。