app.Static(root, prefix) to map a URL prefix to a directory on disk. Lightning registers a wildcard route under that prefix and streams matching files directly to the client.
Basic Usage
./public under the /assets URL prefix:
| File on disk | URL |
|---|---|
./public/logo.png | /assets/logo.png |
./public/css/main.css | /assets/css/main.css |
./public/js/app.js | /assets/js/app.js |
404 Not Found.
Absolute vs. Relative Paths
- Relative path — resolved relative to the directory of the running executable, not the working directory. For example,
"./public"is joined with the path to the compiled binary. - Absolute path — used as-is. Pass an absolute path when you want deterministic resolution regardless of where the binary lives.
During development you typically run
go run main.go from your project root, so ./public resolves relative to the compiled temp binary’s directory, which may not be your project root. If files are not found, switch to an absolute path or embed assets with //go:embed.Complete Example
Directory structure:main.go: