Standard library / embed
embed.FS.ReadFile()
Returns the entire contents of a named embedded file as bytes; paths use io/fs slash syntax. 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
func (f FS) ReadFile(name string) ([]byte, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name or pathname of the target. |
Return Value
([]byte, error)
Returns the entire contents of a named embedded file as bytes; paths use io/fs slash syntax.
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()() b, e := assets.ReadFile("hello.txt") check(e) fmt.Println(string(b))}