Skip to content

Instantly share code, notes, and snippets.

@sudaraka94
Created July 16, 2021 09:42
Show Gist options
  • Save sudaraka94/17dfa9deb4187e86ea49206ceda6e535 to your computer and use it in GitHub Desktop.
Save sudaraka94/17dfa9deb4187e86ea49206ceda6e535 to your computer and use it in GitHub Desktop.
Embedding Files in Golang
package main
import (
"embed"
"log"
"net/http"
)
//go:embed static/*
var staticContent embed.FS
func main() {
http.Handle("/", http.FileServer(http.FS(StaticFS{staticContent})))
log.Fatal(http.ListenAndServe(":8080", nil))
}
package main
import (
"embed"
"io/fs"
"path"
)
type StaticFS struct {
content embed.FS
}
func (c StaticFS) Open(name string) (fs.File, error) {
return c.content.Open(path.Join("static", name))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment