Go net/http Server: Handlers, Routing & Middleware | Tutorial #32

22views
00
14:05
T
Taught by Celeste AI - AI Coding Coach
View on YouTube
Description
Learn how to build HTTP servers in Go! The net/http package provides everything you need to create web APIs - no frameworks required. 📚 What you'll learn: • http.HandleFunc for registering request handlers • http.ResponseWriter for writing responses • *http.Request for reading method, path, headers, and query params • http.ListenAndServe to start the server • http.NewServeMux() for custom routing • Go 1.22 method patterns: "GET /users", "POST /users" • Path parameters: "/users/{id}" with r.PathValue("id") • Exact path match: "GET /{$}" for root only • Middleware pattern: func(http.Handler) http.Handler • Logging middleware with time.Since • Auth middleware with X-API-Key header check • http.Error for error responses with status codes • JSON API with json.NewEncoder(w).Encode • json.NewDecoder(r.Body).Decode for parsing request bodies • w.WriteHeader(http.StatusCreated) for custom status codes ⏱️ Timestamps: 0:00 - Introduction 0:30 - Basic Handlers (HandleFunc, ResponseWriter, Request) 3:03 - ServeMux Routing (method patterns, path parameters) 6:12 - Middleware (logging, authentication) 9:10 - JSON REST API (CRUD endpoints) 13:34 - Recap 14:04 - End 💻 Code: https://github.com/GoCelesteAI/go_http_server 🔗 Previous: Go HTTP Client (Lesson 31) 🔗 Next: Database with SQLite (Lesson 33) 🔔 Subscribe for more Go tutorials!

Tags

golanggoprogrammingtutorialhttphttp servernet/httpREST APIJSONhttp.HandleFunchttp.ServeMuxmiddlewareroutingGo 1.22path parameters
Back to tutorials

Duration

14:05

Published

February 5, 2026

Added to Codegiz

March 15, 2026

Open in YouTube