Created
October 20, 2021 20:04
-
-
Save tomcam/8f6ecc9d1accba9d8af1b980d90db74e to your computer and use it in GitHub Desktop.
Go embed example:/Golang embed tutorial: Embed subdirectory of files at compile time, then display names at runtime
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" | |
"fmt" | |
"io/fs" | |
) | |
// The following embeds all files and subdirectories | |
// from the themes subdirectory of this package into | |
// the executable. So have the subdirectory available | |
// at compile time. Then you can run the finished | |
// executable anywhere and it will display the | |
// list of files even though the themes directory | |
// doesn't exist at runtime. | |
//go:embed themes/* | |
var themeFiles embed.FS | |
// listEmbeddedDir() displays the filenames in the embedded | |
// directory named theme. | |
func listEmbeddedDir(files embed.FS) error { | |
fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error { | |
if err != nil { | |
return err | |
} | |
fmt.Println(path) | |
return nil | |
}) | |
return nil | |
} | |
func main() { | |
listEmbeddedDir(themeFiles) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment