HTTP Server & Client in Go
From the Go cheat sheet · HTTP Server & Client · verified Jul 2026
HTTP Server & Client
net/http server handlers and HTTP client requests
go
// Simple server
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
// GET request
resp, _ := http.Get("https://api.example.com/data")
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)🎯 Go 1.22+ ServeMux supports method + path patterns natively
📌 Always defer resp.Body.Close() on HTTP responses
💡 Set server timeouts to prevent slow-client attacks
⚡ Use http.Client with timeout — default has no timeout