How to Use go

embed to Embed Files in Go Binaries

Use the //go:embed directive to compile files directly into your Go binary for self-contained distribution.

Use the //go:embed directive to embed files directly into your Go binary at compile time. Place the directive immediately before a variable declaration to bind the file path to that variable.

package main

import "embed"

//go:embed data.txt
var content string

func main() {
    // content now holds the file data
}

This approach compiles the file content into the executable, eliminating the need for external file dependencies at runtime.