// Chapter 32 example: serving the files in one folder. // // Run from this folder: // cd exercises/32/04-static-files // go run . package main import ( "fmt" "net/http" ) const homePage = ` Static files

Static files

The colour of this heading comes from a file.

Read the notes

` func handleHome(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, homePage) } func main() { http.HandleFunc("GET /{$}", handleHome) files := http.FileServer(http.Dir("static")) http.Handle("GET /static/", http.StripPrefix("/static/", files)) fmt.Println("Server running at http://localhost:8080") fmt.Println("Press Ctrl+C to stop it.") err := http.ListenAndServe("localhost:8080", nil) if err != nil { fmt.Println("Server stopped:", err) } }