example_test.go 712 B

1234567891011121314151617181920212223242526272829
  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. package httptrace_test
  5. import (
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "net/http/httptrace"
  10. )
  11. func Example() {
  12. req, _ := http.NewRequest("GET", "http://example.com", nil)
  13. trace := &httptrace.ClientTrace{
  14. GotConn: func(connInfo httptrace.GotConnInfo) {
  15. fmt.Printf("Got Conn: %+v\n", connInfo)
  16. },
  17. DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
  18. fmt.Printf("DNS Info: %+v\n", dnsInfo)
  19. },
  20. }
  21. req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  22. _, err := http.DefaultTransport.RoundTrip(req)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. }