Standard library / embed
embed.FS
Read-only compile-time filesystem; safe for concurrent reads and assignment. Zero value has no embedded files. Before building, create hello.txt containing exactly hello (without a newline) next to main.go. Example uses a disposable temporary workspace. Expected stdout: 'hello\n'.
Formal Syntax & Structure
go syntaxgo
type FS struct { /* unexported fields */ }Working Example
Example Snippetgo
package main
import ( "embed" "fmt" "io/fs" "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()() var f fs.FS = assets b, e := fs.ReadFile(f, "hello.txt") check(e) fmt.Println(string(b))}