Serve embedded static files in Go by creating an embed.FS variable and passing it to http.FileServer.
Serve embedded static files by registering a http.FileServer handler with the embed.FS filesystem.
package main
import (
"embed"
"net/http"
)
//go:embed static
var staticFS embed.FS
func main() {
http.Handle("/", http.FileServer(http.FS(staticFS)))
http.ListenAndServe(":8080", nil)
}
Serving embedded static files from an HTTP server bundles your website files directly into your program so you only need to distribute a single executable. It works like a self-contained app that carries its own assets, eliminating the need for external folders or complex deployment steps.