How to Serve HTML Pages from a Go HTTP Server

Web
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.