A static file is a preexisting asset served to clients rather than generated for each request. In a Go web server, common static files include CSS, JavaScript, images, and other application assets. section 10.0 section 10.1
Choose a serving arrangement according to deployment needs:
- Serve a directory with http.FileServer and http.Dir; the handler follows filesystem permissions and can return 304 Not Modified for matching If-Modified-Since requests. section 10.1
- Serve one known file with http.ServeFile from a custom handler, or mount a directory under a URL prefix with http.StripPrefix. section 10.1
- Embed assets with Go's embed package when the executable should be self-contained; expose an embedded filesystem through http.FS. section 10.1.3
- Serve assets from a CDN or another location by passing the environment-specific URL into templates as runtime configuration. section 10.1.4
Serving an embedded asset directory
A Go 1.16-or-later application can embed a files directory and serve it without distributing a separate asset directory: `//go:embed files`, `var f embed.FS`, then `http.FileServer(http.FS(f))`. Check filesystem paths when using external directories, because a missing directory can produce 404 responses without an initialization error. section 10.1
Listing 10.5 Embedding files in binaries with go:embed
package main
import (
"embed" #1
"net/http"
)
//go:embed files #2
var f embed.FS #3
func main() {
if err := http.ListenAndServe(":8088",
http.FileServer(http.FS(f))); err != nil { #4
panic(err)
}
}
Inspect the complete embedded-filesystem example, including the compiler directive and http.FS adapter. section 10.1.3





