{"record":{"id":"aa8aba83b3a6a8b6","repo":"golang/go","slug":"go-embed-cannot-apply-to-var-with-initializer","errorCode":null,"errorMessage":"go:embed cannot apply to var with initializer","messagePattern":"go:embed cannot apply to var with initializer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":469,"sourceCode":"// 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":451,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L451-L482","documentation":"Thrown by checkEmbed when the embedded var has an initializer (decl.Values != nil). Embedded variables are populated by the compiler from the embedded files, so supplying a value (e.g. `var x = []byte{}`) conflicts with the directive's semantics.","triggerScenarios":"Writing `//go:embed f.txt\\nvar x = []byte{}` or any var under //go:embed that includes an `=` initializer. checkEmbed sees decl.Values != nil.","commonSituations":"Leftover initializer from converting a normal var to an embedded one; IDE auto-complete inserting `= nil`; copy-paste from a non-embed template.","solutions":["Remove the initializer; declare the var with just its type, e.g. `var x embed.FS`.","If you need default content, embed an additional fallback file rather than initializing."],"exampleFix":"// before\n//go:embed f.txt\nvar data = []byte{}\n// after\n//go:embed f.txt\nvar data []byte","handlingStrategy":"validation","validationCode":"// An embed var must have a type and no initializer.\nfunc validEmbedVar(hasType bool, hasInit bool) bool { return hasType && !hasInit }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare embedded vars as `var X embed.FS` or `var X []byte` with no `=`.","Remove initializers when converting a normal var to an embedded one.","Watch for IDE auto-complete inserting `= nil`."],"tags":["compiler","go-embed","var-declaration","initializer","go-directives"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}