w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello world!")) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Header("X-Foo", "bar") w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) }
w.Write([]byte("Hello world!")) } type ResponseWriter interface { // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are trailers. Header() Header Write([]byte) (int, error) // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). WriteHeader(int) } var handler = func(w http.ResponseWriter, r *http.Request) { w.Header("X-Foo", "bar") w.WriteHeader(http.StatusCreated) w.Write([]byte("Hello world!")) // ❌ w.WriteHeader(http.StatusBadRequest) after a call to Write // ❌ w.Header("X-Foo", "qux") after a call to WriteHeader }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { http.DefaultClient.Timeout = 30 * time.Second }
w.Write([]byte("Hello world!")) } func main() { s := httptest.NewServer(http.HandlerFunc(sleepHandler)) defer s.Close() resp, err := http.Get(s.URL) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // A Client is an HTTP client. Its zero value (DefaultClient) is a // usable client that uses DefaultTransport. // // The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // ... var DefaultClient = &Client{} type Client struct { Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar // A Timeout of zero means no timeout. Timeout time.Duration } func init() { http.DefaultClient.Timeout = 30 * time.Second // Modify if needed. http.DefaultTransport = &http.Transport{ // ... } }