How to Serve Static Files in Go

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