Go 1.16 introduced the embed package which allows serving static files from directly within the binary without the need for any external package.
Here’s two example’s showing how to serve from the root URL path.
main.go This was my original attempt at serving a subdirectory at the root of the URL before I realized what the new io/fs package provided. This method does however allow the addition of headers and other possible modifications.
- View the complete gist
- Download example.go or main.go or…both
- Create the directories
mkdir -p public/css
- Download index.html within the public directory.
- Download style.css within the public/css directory.
- Build and run example.go or main.go.