How to Use go

embed to Embed Templates in Your Binary

Web
Embed template files into your Go binary using the //go:embed directive and embed.FS type.

Use the //go:embed directive to embed files directly into your binary at compile time. Place the directive above a variable declaration, specifying the file path pattern relative to the source file.

package main

import "embed"

//go:embed templates/*.html
var TemplateFS embed.FS

func main() {
    // TemplateFS now contains all .html files in the templates directory
}

This creates a read-only filesystem (embed.FS) that you can use with html/template or io/fs to read files without needing external assets.