Standard library / embed
embed.FS.ReadDir()
Returns the directory entries for an embedded path; entries are sorted by filename. Before building, create hello.txt containing exactly hello (without a newline) next to main.go. Example uses a disposable temporary workspace. Expected stdout: 'hello.txt\n'.
Formal Syntax & Structure
go syntaxgo
func (f FS) ReadDir(name string) ([]fs.DirEntry, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name or pathname of the target. |
Return Value
([]fs.DirEntry, error)
Returns the directory entries for an embedded path; entries are sorted by filename.
Working Example
Example Snippetgo
package main
import ( "embed" "fmt" "os")
func check(e error) { if e != nil { panic(e) }}func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}
//go:embed hello.txtvar assets embed.FS
func main() { defer workspace()(); d, e := assets.ReadDir("."); check(e); fmt.Println(d[0].Name()) }