How to Embed Files in a Go Binary with go

embed

Cli
Embed files into a Go binary using the //go:embed directive to include static assets at compile time.

Use the //go:embed directive to embed files directly into your Go binary at compile time.

package main

import "embed"

//go:embed data.txt
var content []byte

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

For directories, use a wildcard pattern:

//go:embed assets/*
var assets embed.FS