Serve static files in Go using http.FileServer and http.Dir to map a local directory to a web path.
Use http.FileServer with http.Dir to serve files from a directory.
package main
import (
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
}
This serves files from the ./static directory at the root path / on port 8080.
Serving static files in Go creates a simple web server that sends files from your computer to a web browser. Think of it like a digital file cabinet where the browser asks for a specific document, and the server hands it over immediately. You use this when you need to host images, stylesheets, or HTML pages without a complex backend.