{"record":{"id":"555918b92ca5cb99","repo":"golang/go","slug":"go-embed-requires-import-embed-or-import-emb","errorCode":null,"errorMessage":"go:embed requires import \"embed\" (or import _ \"embed\", if package is not used)","messagePattern":"go:embed requires import \"embed\" \\(or import _ \"embed\", if package is not used\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":465,"sourceCode":"\treturn list, nil\n}\n\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":447,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L447-L482","documentation":"Thrown by checkEmbed (src/cmd/compile/internal/noder/noder.go) as the first case of its switch: a //go:embed directive is present on a declaration but the enclosing package has not imported the embed package. The compiler requires `import \"embed\"` (or `import _ \"embed\"`) so that the directive is opt-in and the embed API is available.","triggerScenarios":"A var declaration carries a `//go:embed` comment but the file lacks `import \"embed\"`. checkEmbed is called with haveEmbed==false.","commonSituations":"Copying an embed example without the import; removing the embed import during cleanup while leaving the directive; using //go:embed before Go 1.16 semantics were understood; tooling that injects directives without managing imports.","solutions":["Add `import \"embed\"` to the file, or `import _ \"embed\"` if you only use the directive and not the embed.FS API.","Remove the //go:embed directive if embedding was not intended.","Confirm the module's go.mod declares go 1.16 or later."],"exampleFix":"// before\n//go:embed assets\nvar assets []byte\n// after\nimport _ \"embed\"\n\n//go:embed assets\nvar assets []byte","handlingStrategy":"validation","validationCode":"// Ensure the embed import is present whenever a //go:embed directive is used.\nfunc hasEmbedImport(imports []string) bool {\n    for _, im := range imports { if im == \"embed\" { return true } }\n    return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When adding a //go:embed directive, add `import _ \"embed\"` in the same change.","Use goimports/go fix to keep imports consistent after edits.","Confirm go.mod declares go 1.16+ before using embed."],"tags":["compiler","go-embed","imports","go-directives"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}