Static file serving
Serve static files explicitly, keep the URL subtree narrow, and distinguish immutable assets from runtime configuration.
Canonical guidance
- serve static assets from a narrow, explicit path
- choose between disk and embedded assets deliberately
- treat asset exposure as part of the public surface
Use when
- CSS, JS, images, and fonts
- embedded front-end assets
- admin or documentation servers
Avoid
- serving broad directory trees accidentally
- mixing mutable runtime data with published assets
- ad hoc file serving inside many unrelated handlers
Preferred pattern
- mount one file server under a known prefix and keep asset lookup centralized
Anti-pattern
- pointing a file server at a project root because the needed directory is somewhere under it
Explanation: This is tempting during development, but it widens the public surface and risks leaking unintended files.
Why
- static serving is simple only when the asset boundary is simple
Related pages
Sources
- net/http package - Go Team
- embed package - Go Team