Created
July 16, 2021 09:42
-
-
Save sudaraka94/17dfa9deb4187e86ea49206ceda6e535 to your computer and use it in GitHub Desktop.
Embedding Files in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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