http.Transport类型

  1. > ```go
  2. > type Transport struct {
  3. > idleMu sync.Mutex
  4. > wantIdle bool
  5. > idleConn map[connectMethodKey][]*persistConn
  6. > idleConnCh map[connectMethodKey]chan *persistConn
  7. > idleLRU connLRU
  8. > reqMu sync.Mutex
  9. > reqCanceler map[*Request]func(error)
  10. > altMu sync.Mutex
  11. > altProto atomic.Value
  12. > Proxy func(*Request) (*url.URL, error)
  13. > DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
  14. > Dial func(network, addr string) (net.Conn, error)
  15. > DialTLS func(network, addr string) (net.Conn, error)
  16. > TLSClientConfig *tls.Config
  17. > TLSHandshakeTimeout time.Duration
  18. > DisableKeepAlives bool
  19. > DisableCompression bool
  20. > MaxIdleConns int
  21. > MaxIdleConnsPerHost int
  22. > IdleConnTimeout time.Duration
  23. > ResponseHeaderTimeout time.Duration
  24. > ExpectContinueTimeout time.Duration
  25. > TLSNextProto map[string]func(authority string, c *tls.Conn) RoundTripper
  26. > ProxyConnectHeader Header
  27. > MaxResponseHeaderBytes int64
  28. > nextProtoOnce sync.Once
  29. > h2transport *http2Transport
  30. > }
  31. >

如您所见,http.Transport是一个包含大量字段的复杂结构。好消息是在编写HTTP相关程序时,并不需要经常使用http.Transport结构,并且在使用时不需要处理它的所有字段。

http.Transport结构实现了http.RoundTripper接口,并且支持HTTP、HTTPS和HTTP代理的模式。不过http.Transport是一个低级别的结构,本章中使用的http.Client结构则是一个高级别的HTTP客户端实现。