Wednesday, November 02, 2022

GoLang embed

 How to embed files into Go binaries - Stack Overflow

Starting with Go 1.16, released in Feb 2021, you can use the go:embed directive:
import "embed"

//go:embed hello.txt
var s string
print(s)

//go:embed hello.txt
var b []byte
print(string(b))

//go:embed hello.txt
var f embed.FS
data, _ := f.ReadFile("hello.txt")
print(string(data))

Documentation: https://pkg.go.dev/embed
Example: https://blog.carlmjohnson.net/post/2021/how-to-use-go-embed/
How to Use go:embed in Go | The GoLand Blog