{"record":{"id":"afec3d5294a6e466","repo":"golang/go","slug":"go-embed-requires-go1-16-or-later-lang-was-set-t","errorCode":null,"errorMessage":"go:embed requires go1.16 or later (-lang was set to %s; check go.mod)","messagePattern":"go:embed requires go1\\.16 or later \\(-lang was set to (.+?); check go\\.mod\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/noder/noder.go","lineNumber":476,"sourceCode":"\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":458,"sourceCodeEnd":482,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/noder/noder.go#L458-L482","documentation":"The //go:embed directive was introduced in Go 1.16. The compiler checks the effective language version (derived from go.mod's go directive or the -lang compiler flag) via types.AllowsGoVersion(1, 16). If the version is below 1.16 and an embed directive is encountered on a var declaration, this error is returned with the current -lang value.","triggerScenarios":"A go.mod file declaring go 1.15 or lower combined with //go:embed directives in source files. Also triggered by explicitly passing -lang go1.15 (or lower) to the compiler while using embed directives.","commonSituations":"Adding embed directives to an existing project without updating go.mod's go directive. Working in a monorepo or vendored dependency where the language version is pinned below 1.16. CI pipelines that pass -lang explicitly.","solutions":["Update go.mod to go 1.16 or higher: edit the go directive to 'go 1.21' (or your toolchain version)","Remove any explicit -lang compiler flag or set it to go1.16+","If you cannot raise the language version, remove the embed directives and load files at runtime with os.ReadFile"],"exampleFix":"// before (go.mod)\ngo 1.15\n\n// after (go.mod)\ngo 1.21","handlingStrategy":"validation","validationCode":"// Check go.mod version supports embed before using it\nimport (\n    \"os\"\n    \"strings\"\n)\n\nfunc checkGoModVersion() (major, minor int, err error) {\n    data, err := os.ReadFile(\"go.mod\")\n    if err != nil {\n        return 0, 0, err\n    }\n    for _, line := range strings.Split(string(data), \"\\n\") {\n        line = strings.TrimSpace(line)\n        if strings.HasPrefix(line, \"go \") {\n            v := strings.TrimPrefix(line, \"go \")\n            parts := strings.Split(v, \".\")\n            if len(parts) >= 2 {\n                fmt.Sscanf(parts[0], \"%d\", &major)\n                fmt.Sscanf(parts[1], \"%d\", &minor)\n                return major, minor, nil\n            }\n        }\n    }\n    return 0, 0, fmt.Errorf(\"no go directive in go.mod\")\n}\n\n// Usage: ensure major > 1 || (major == 1 && minor >= 16) before using embed","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set go.mod's go directive to at least the version that introduced the features you use","Keep go.mod's go directive updated when upgrading your Go toolchain","Do not pass -lang explicitly in build scripts unless necessary — let go.mod control it","Document minimum Go version requirements in your project README"],"tags":["go-compiler","go-embed","go-version","go-mod","configuration"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}