{"record":{"id":"900e91cce79b1708","repo":"golang/go","slug":"go-embed-cannot-apply-to-var-without-type","errorCode":null,"errorMessage":"go:embed cannot apply to var without type","messagePattern":"go:embed cannot apply to var without type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":472,"sourceCode":"var 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":454,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L454-L482","documentation":"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.","triggerScenarios":"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.","commonSituations":"Tooling that synthesizes an AST without setting Type; an internal compiler refactor that relaxes parser invariants; extremely rare in hand-written code.","solutions":["Add an explicit type to the var: []byte or embed.FS.","If this fires from generated/tool-modified AST, ensure the generator emits a Type node.","Report a compiler bug if it occurs with valid hand-written source, since the parser should prevent it."],"exampleFix":"// before (no type)\n//go:embed f.txt\nvar data\n// after\n//go:embed f.txt\nvar data []byte","handlingStrategy":"validation","validationCode":"// Embed var requires an explicit type node.\nfunc embedVarHasType(typeIsSet bool) bool { return typeIsSet }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write the type explicitly on embedded vars ([]byte or embed.FS).","If generating AST, ensure the Type field is populated for embed vars.","Report a compiler bug if this fires on valid hand-written source."],"tags":["compiler","go-embed","var-declaration","type-annotation","go-directives"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}