golang/go · error

go:embed requires import "embed" (or import _ "embed", if pa

Error message

go:embed requires import "embed" (or import _ "embed", if package is not used)

What it means

Thrown by checkEmbed (src/cmd/compile/internal/noder/noder.go) as the first case of its switch: a //go:embed directive is present on a declaration but the enclosing package has not imported the embed package. The compiler requires `import "embed"` (or `import _ "embed"`) so that the directive is opt-in and the embed API is available.

Source

Thrown at src/cmd/compile/internal/noder/noder.go:465

	return list, nil
}

// A function named init is a special case.
// It is called by the initialization before main is run.
// To make it unique within a package and also uncallable,
// the name, normally "pkg.init", is altered to "pkg.init.0".
var renameinitgen int

func Renameinit() *types.Sym {
	s := typecheck.LookupNum("init.", renameinitgen)
	renameinitgen++
	return s
}

func checkEmbed(decl *syntax.VarDecl, haveEmbed, withinFunc bool) error {
	switch {
	case !haveEmbed:
		return errors.New("go:embed requires import \"embed\" (or import _ \"embed\", if package is not used)")
	case len(decl.NameList) > 1:
		return errors.New("go:embed cannot apply to multiple vars")
	case decl.Values != nil:
		return errors.New("go:embed cannot apply to var with initializer")
	case decl.Type == nil:
		// Should not happen, since Values == nil now.
		return errors.New("go:embed cannot apply to var without type")
	case withinFunc:
		return errors.New("go:embed cannot apply to var inside func")
	case !types.AllowsGoVersion(1, 16):
		return fmt.Errorf("go:embed requires go1.16 or later (-lang was set to %s; check go.mod)", base.Flag.Lang)

	default:
		return nil
	}
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Add `import "embed"` to the file, or `import _ "embed"` if you only use the directive and not the embed.FS API.
  2. Remove the //go:embed directive if embedding was not intended.
  3. Confirm the module's go.mod declares go 1.16 or later.

Example fix

// before
//go:embed assets
var assets []byte
// after
import _ "embed"

//go:embed assets
var assets []byte
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the embed import is present whenever a //go:embed directive is used.
func hasEmbedImport(imports []string) bool {
    for _, im := range imports { if im == "embed" { return true } }
    return false
}

Prevention

When it happens

Trigger: A var declaration carries a `//go:embed` comment but the file lacks `import "embed"`. checkEmbed is called with haveEmbed==false.

Common situations: Copying an embed example without the import; removing the embed import during cleanup while leaving the directive; using //go:embed before Go 1.16 semantics were understood; tooling that injects directives without managing imports.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/555918b92ca5cb99. Report an issue: GitHub.