{"record":{"id":"10081df4500ec671","repo":"golang/go","slug":"var-q-is-not-a-valid-identifier","errorCode":null,"errorMessage":"-var: %q is not a valid identifier","messagePattern":"-var: %q is not a valid identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/cover/cover.go","lineNumber":147,"sourceCode":"}\n\n// parseFlags sets the profile and counterStmt globals and performs validations.\nfunc parseFlags() error {\n\tprofile = *htmlOut\n\tif *funcOut != \"\" {\n\t\tif profile != \"\" {\n\t\t\treturn fmt.Errorf(\"too many options\")\n\t\t}\n\t\tprofile = *funcOut\n\t}\n\n\t// Must either display a profile or rewrite Go source.\n\tif (profile == \"\") == (*mode == \"\") {\n\t\treturn fmt.Errorf(\"too many options\")\n\t}\n\n\tif *varVar != \"\" && !token.IsIdentifier(*varVar) {\n\t\treturn fmt.Errorf(\"-var: %q is not a valid identifier\", *varVar)\n\t}\n\n\tif *mode != \"\" {\n\t\tswitch *mode {\n\t\tcase \"set\":\n\t\t\tcounterStmt = setCounterStmt\n\t\t\tcmode = coverage.CtrModeSet\n\t\tcase \"count\":\n\t\t\tcounterStmt = incCounterStmt\n\t\t\tcmode = coverage.CtrModeCount\n\t\tcase \"atomic\":\n\t\t\tcounterStmt = atomicCounterStmt\n\t\t\tcmode = coverage.CtrModeAtomic\n\t\tcase \"regonly\":\n\t\t\tcounterStmt = nil\n\t\t\tcmode = coverage.CtrModeRegOnly\n\t\tcase \"testmain\":\n\t\t\tcounterStmt = nil","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/cover/cover.go#L129-L165","documentation":"Thrown by 'go tool cover' parseFlags when -var is set to a string that is not a valid Go identifier (checked via token.IsIdentifier). The -var flag names the coverage counter variable injected into instrumented source, so it must be a legal Go identifier to produce compilable code.","triggerScenarios":"`go tool cover -mode=set -var=1Coverage` or `-var=Cov-Var` or `-var=` (empty after the guard) — any value failing token.IsIdentifier while non-empty.","commonSituations":"Variable names starting with a digit, containing hyphens/spaces/punctuation, or using reserved symbols. Custom build scripts templating the -var value unsafely.","solutions":["Use a valid Go identifier for -var (letters/digits/underscore, not starting with a digit)","Leave -var unset to use the default GoCover","Validate the name in your build script with a regex like ^[A-Za-z_][A-Za-z0-9_]*$ before passing it"],"exampleFix":"# before\ngo tool cover -mode=set -var=Cov-Count file.go\n# after\ngo tool cover -mode=set -var=CovCount file.go","handlingStrategy":"validation","validationCode":"import re\nname = \"CovCount\"\nif not re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', name):\n    raise SystemExit(f\"invalid -var identifier: {name}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate identifier names with a regex before passing to -var","Prefer the default GoCover unless you need a custom name"],"tags":["go-toolchain","cover","cli-flags","identifier","validation"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}