The most powerful package in Go's standard library for web development. Provides both an HTTP/HTTPS client and a production-ready HTTP server — no framework needed for many use cases. Handles routing, headers, cookies, TLS, and streaming.
Import: import "net/http" | Docs: pkg.go.dev/net/http
| Name | Signature / Value | Description |
|---|---|---|
| http.Get | func Get(url string) (resp *Response, err error) | Issue HTTP GET request |
| http.Post | func Post(url, contentType string, body io.Reader) (*Response, error) | Issue HTTP POST request |
| http.PostForm | func PostForm(url string, data url.Values) (*Response, error) | POST form data |
| http.NewRequest | func NewRequest(method, url string, body io.Reader) (*Request, error) | Build custom HTTP request |
| http.DefaultClient | var DefaultClient = &Client{} | Package-level HTTP client |
| client.Do | func (c *Client) Do(req *Request) (*Response, error) | Execute a custom request |
| resp.Body | io.ReadCloser | Response body — always defer resp.Body.Close() |
| resp.StatusCode | int | HTTP status code (200, 404, 500 etc.) |
| resp.Header | Header | Response headers map |
| http.ListenAndServe | func ListenAndServe(addr string, handler Handler) error | Start HTTP server on addr |
| http.ListenAndServeTLS | func ListenAndServeTLS(addr, cert, key string, h Handler) error | Start HTTPS server |
| http.HandleFunc | func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) | Register URL pattern handler |
| http.Handle | func Handle(pattern string, handler Handler) | Register Handler interface |
| http.NewServeMux | func NewServeMux() *ServeMux | Create new HTTP request multiplexer |
| http.ResponseWriter | interface | Interface for writing HTTP responses |
| w.WriteHeader | func (w ResponseWriter) WriteHeader(statusCode int) | Set HTTP status code |
| w.Header().Set | func (h Header) Set(key, value string) | Set response header |
| r.URL.Query | func (u *URL) Query() Values | Parse URL query string |
| r.FormValue | func (r *Request) FormValue(key string) string | Get form or query parameter |
| r.Method | string | HTTP method (GET, POST etc.) |
| r.Header.Get | func (h Header) Get(key string) string | Get request header value |
| http.StatusOK | const = 200 | HTTP 200 OK |
| http.StatusNotFound | const = 404 | HTTP 404 Not Found |
| http.StatusInternalServerError | const = 500 | HTTP 500 Internal Server Error |
| http.StatusCreated | const = 201 | HTTP 201 Created |
| http.Redirect | func Redirect(w ResponseWriter, r *Request, url string, code int) | Send redirect response |
| http.Error | func Error(w ResponseWriter, error string, code int) | Send error response |
| http.ServeFile | func ServeFile(w ResponseWriter, r *Request, name string) | Serve a file |
| http.FileServer | func FileServer(root FileSystem) Handler | HTTP handler for static files |
| http.StripPrefix | func StripPrefix(prefix string, h Handler) Handler | Strip URL prefix before passing to handler |
Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.
package main
import (
"fmt"
"io"
"log"
"net/http"
"time"
)
// --- Handler functions ---
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
name := r.URL.Query().Get("name")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "<h1>Hello, %s!</h1><p>Method: %s</p>", name, r.Method)
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status":"ok","time":"%s"}`, time.Now().Format(time.RFC3339))
}
// --- HTTP client ---
func fetchURL(url string) {
resp, err := http.Get(url)
if err != nil {
log.Println("GET error:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Status: %d Body: %s\n", resp.StatusCode, string(body)[:80])
}
func main() {
// --- Server ---
mux := http.NewServeMux()
mux.HandleFunc("/", homeHandler)
mux.HandleFunc("/api", apiHandler)
// Serve static files
fs := http.FileServer(http.Dir("./static"))
mux.Handle("/static/", http.StripPrefix("/static/", fs))
server := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
fmt.Println("Server starting on http://localhost:8080")
fmt.Println("Try: http://localhost:8080/?name=MyWebUniversity")
fmt.Println("Try: http://localhost:8080/api")
log.Fatal(server.ListenAndServe())
}
// go run main.go
// curl http://localhost:8080/api