This is a simple Go server that serves files from an embed.FS file system. This is useful if you want to embed static files (like HTML, CSS, and JavaScript) directly into your Go binary.
- Clone the repository to your local machine.
- Add the files you want to serve to the static directory.
- Run the server using the go run command:
# clone the repo
git clone [email protected]:pyadav/a-guide-to-embedding-static-files-in-go.git
go run main.go
You can customize the server to serve files from a different directory or port by changing the values in embed.go. For example, to serve files from a directory called public, change this line:
//go:embed all:dist
var assets embed.FS
to this:
//go:embed all:public
var assets embed.FS
You can also change the port by modifying the argument to http.ListenAndServe in main.go
. For example, to use port 3000 instead of the default 8080, change this line:
const port = ":8080"
to this:
const port = ":3000"
This program is licensed under the Apache 2.0. See the LICENSE file for more information.