🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 语法 可追踪的函数 ``` type ClientTrace struct { GetConn func(hostPort string) GotConn func(GotConnInfo) PutIdleConn func(err error) GotFirstResponseByte func() Got100Continue func() DNSStart func(DNSStartInfo) DNSDone func(DNSDoneInfo) ConnectStart func(network, addr string) ConnectDone func(network, addr string, err error) TLSHandshakeStart func() TLSHandshakeDone func(tls.ConnectionState, error) WroteHeaders func() Wait100Continue func() WroteRequest func(WroteRequestInfo) } ``` ## 实例 ### 设置监听函数 ``` req, _ := http.NewRequest("GET", "http://example.com", nil) trace := &httptrace.ClientTrace{ GotConn: func(connInfo httptrace.GotConnInfo) { fmt.Printf("Got Conn: %+v\n", connInfo) }, DNSStart: func(info httptrace.DNSStartInfo) { fmt.Printf("DNSStart %+v\n", info) }, DNSDone: func(dnsInfo httptrace.DNSDoneInfo) { fmt.Printf("DNS Info: %+v\n", dnsInfo) }, ConnectStart: func(network, addr string) { fmt.Printf("connectStart %+v%+v\n", network, addr) }, } req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) res, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } fmt.Printf("%+v\n", res.Status) ```