golang/go · error

go:embed cannot apply to multiple vars

Error message

go:embed cannot apply to multiple vars

What it means

Thrown by checkEmbed when a single //go:embed directive is applied to a var declaration that declares more than one name (len(decl.NameList) > 1). Embedding binds one directive to one variable, so multi-name declarations like `var a, b = ...` are ambiguous and rejected.

Source

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

// 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. Split into one var per directive, each with its own //go:embed line.
  2. Embed into a single embed.FS to hold multiple files under one var.

Example fix

// before
//go:embed a.txt b.txt
var a, b []byte
// after (one directive each)
//go:embed a.txt
var a []byte
//go:embed b.txt
var b []byte
// ...or use one FS:
//go:embed a.txt b.txt
var files embed.FS
Defensive patterns

Strategy: validation

Validate before calling

// Ensure an embed var declaration has exactly one name.
func singleEmbedName(names []string) bool { return len(names) == 1 }

Prevention

When it happens

Trigger: Writing `//go:embed f.txt\nvar a, b fs.FS` or any `var x, y ...` under a //go:embed line. checkEmbed sees NameList length > 1.

Common situations: Trying to embed multiple files into multiple vars with one directive; refactoring a var group and accidentally merging an embedded var with another; misunderstanding that //go:embed targets a single identifier.

Related errors


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