golang/go · error

go:embed cannot apply to var without type

Error message

go:embed cannot apply to var without type

What it means

Thrown by checkEmbed when the embedded var has no explicit type (decl.Type == nil). The comment in source notes this 'should not happen' since an initializer-less var requires a type by the parser, but checkEmbed guards defensively. Embedding needs a known type ([]byte or embed.FS) to populate.

Source

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

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 an explicit type to the var: []byte or embed.FS.
  2. If this fires from generated/tool-modified AST, ensure the generator emits a Type node.
  3. Report a compiler bug if it occurs with valid hand-written source, since the parser should prevent it.

Example fix

// before (no type)
//go:embed f.txt
var data
// after
//go:embed f.txt
var data []byte
Defensive patterns

Strategy: validation

Validate before calling

// Embed var requires an explicit type node.
func embedVarHasType(typeIsSet bool) bool { return typeIsSet }

Prevention

When it happens

Trigger: A //go:embed var declaration reaches checkEmbed with both no initializer and no type. In practice unreachable through normal source; could surface via malformed AST input or a future parser change.

Common situations: Tooling that synthesizes an AST without setting Type; an internal compiler refactor that relaxes parser invariants; extremely rare in hand-written code.

Related errors


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