Serve HTML pages from a Go HTTP server using http.FileServer and http.Dir to handle static file requests.
Use http.FileServer with http.Dir to serve static HTML files from a directory.
package main
import (
"log"
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":8080", nil))
}
Place your HTML files in a folder named static relative to your executable.
A Go HTTP server starts a web service that automatically finds and sends HTML files to visitors. It works like a digital file cabinet where the browser requests a specific file, and the server hands it over immediately. You use this when you want to host a simple website without writing custom code for every page.