{"record":{"id":"e3d769535087d7ce","repo":"golang/go","slug":"go-embed-cannot-apply-to-multiple-vars","errorCode":null,"errorMessage":"go:embed cannot apply to multiple vars","messagePattern":"go:embed cannot apply to multiple vars","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":467,"sourceCode":"\n// A function named init is a special case.\n// It is called by the initialization before main is run.\n// To make it unique within a package and also uncallable,\n// the name, normally \"pkg.init\", is altered to \"pkg.init.0\".\nvar renameinitgen int\n\nfunc Renameinit() *types.Sym {\n\ts := typecheck.LookupNum(\"init.\", renameinitgen)\n\trenameinitgen++\n\treturn s\n}\n\nfunc checkEmbed(decl *syntax.VarDecl, haveEmbed, withinFunc bool) error {\n\tswitch {\n\tcase !haveEmbed:\n\t\treturn errors.New(\"go:embed requires import \\\"embed\\\" (or import _ \\\"embed\\\", if package is not used)\")\n\tcase len(decl.NameList) > 1:\n\t\treturn errors.New(\"go:embed cannot apply to multiple vars\")\n\tcase decl.Values != nil:\n\t\treturn errors.New(\"go:embed cannot apply to var with initializer\")\n\tcase decl.Type == nil:\n\t\t// Should not happen, since Values == nil now.\n\t\treturn errors.New(\"go:embed cannot apply to var without type\")\n\tcase withinFunc:\n\t\treturn errors.New(\"go:embed cannot apply to var inside func\")\n\tcase !types.AllowsGoVersion(1, 16):\n\t\treturn fmt.Errorf(\"go:embed requires go1.16 or later (-lang was set to %s; check go.mod)\", base.Flag.Lang)\n\n\tdefault:\n\t\treturn nil\n\t}\n}\n","sourceCodeStart":449,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L449-L482","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Split into one var per directive, each with its own //go:embed line.","Embed into a single embed.FS to hold multiple files under one var."],"exampleFix":"// before\n//go:embed a.txt b.txt\nvar a, b []byte\n// after (one directive each)\n//go:embed a.txt\nvar a []byte\n//go:embed b.txt\nvar b []byte\n// ...or use one FS:\n//go:embed a.txt b.txt\nvar files embed.FS","handlingStrategy":"validation","validationCode":"// Ensure an embed var declaration has exactly one name.\nfunc singleEmbedName(names []string) bool { return len(names) == 1 }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Give each embedded var its own //go:embed directive and declaration.","Use a single embed.FS when you need multiple files under one var.","Review refactors that merge var groups to avoid co-declaring embedded vars."],"tags":["compiler","go-embed","var-declaration","go-directives"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}