{"record":{"id":"6b8a6d02faeb40bb","repo":"golang/go","slug":"go-embed-cannot-apply-to-var-inside-func","errorCode":null,"errorMessage":"go:embed cannot apply to var inside func","messagePattern":"go:embed cannot apply to var inside func","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":474,"sourceCode":"func 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":456,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L456-L482","documentation":"Thrown by checkEmbed when the embedded var declaration appears inside a function body (withinFunc == true). //go:embed only operates on package-level variables because the embed mechanism runs at compile time and populates package initializers, which cannot be local.","triggerScenarios":"Placing a `//go:embed` directive above a var declared inside a func. checkEmbed is called with withinFunc==true.","commonSituations":"Trying to embed a file per-function-call for scoped access; refactoring that moved an embedded var into a function; misunderstanding that embed is a package-level facility.","solutions":["Move the var (and its //go:embed directive) to package scope.","Access the package-level embedded value from within the function."],"exampleFix":"// before\nfunc load() {\n    //go:embed f.txt\n    var data []byte\n    _ = data\n}\n// after\n//go:embed f.txt\nvar data []byte\n\nfunc load() { _ = data }","handlingStrategy":"validation","validationCode":"// Ensure embed vars are declared at package scope, not inside a func.\nfunc embedAtPackageScope(isInsideFunc bool) bool { return !isInsideFunc }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep //go:embed directives on package-level vars only.","Access package-level embedded data from within functions instead of re-declaring locally.","Review refactors that move vars into functions for locality."],"tags":["compiler","go-embed","scope","package-level","go-directives"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}