github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/http/httptrace/example_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package httptrace_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"net/http"
    11  	"net/http/httptrace"
    12  )
    13  
    14  func Example() {
    15  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    16  	trace := &httptrace.ClientTrace{
    17  		GotConn: func(connInfo httptrace.GotConnInfo) {
    18  			fmt.Printf("Got Conn: %+v\n", connInfo)
    19  		},
    20  		DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
    21  			fmt.Printf("DNS Info: %+v\n", dnsInfo)
    22  		},
    23  	}
    24  	req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
    25  	_, err := http.DefaultTransport.RoundTrip(req)
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  }