Skip to content

Instantly share code, notes, and snippets.

@BrandonClapp
Last active October 18, 2022 15:18
Show Gist options
  • Save BrandonClapp/2e47f46b2a0b7d16885abede0ea557c3 to your computer and use it in GitHub Desktop.
Save BrandonClapp/2e47f46b2a0b7d16885abede0ea557c3 to your computer and use it in GitHub Desktop.
Loop over files in an embed FS
package main
import (
"embed"
"fmt"
"io/fs"
)
//go:embed template/*
var template embed.FS
func embedExample() {
fs.WalkDir(template, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// We only care about files
if d.IsDir() {
return nil
}
buf, err := fs.ReadFile(template, path)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(string(buf))
// fmt.Printf("path=%q, isDir=%v\n", path, d.IsDir())
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment