Lab 09: HTTP Client & Server
Overview
Step 1: Basic HTTP Server
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!\n", r.URL.Query().Get("name"))
}
func main() {
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome to Go HTTP!")
})
fmt.Println("server listening on :8080")
http.ListenAndServe(":8080", nil) // uses DefaultServeMux
}Step 2: http.ServeMux — Custom Router
Step 3: JSON Request/Response Handling
Step 4: Middleware Pattern
Step 5: HTTP Client
Step 6: Request Context and Headers
Step 7: Error Handling and Status Codes
Step 8: Capstone — REST API with Middleware
Summary
Component
Key API
Notes
Last updated
