Standard library / embed

embed.directive

A package-scope variable of string, byte slice or embed.FS type receives files at build time. Patterns are package-relative slash paths and each must match. Files cannot be loaded dynamically with the directive. 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
//go:embed hello.txt

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 message string
func main() { defer workspace()(); fmt.Println(message) }
Go Reference