{"record":{"id":"440370e8304122be","repo":"beego/beego","slug":"key-is-empty","errorCode":null,"errorMessage":"key is empty","messagePattern":"key is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/config/ini.go","lineNumber":451,"sourceCode":"\n\t\t\t// Put a line between sections.\n\t\t\tif _, err = buf.WriteString(lineBreak); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\t_, err = buf.WriteTo(f)\n\treturn err\n}\n\n// Set writes a new value for key.\n// if write to one section, the key need be \"section::key\".\n// if the section is not existed, it panics.\nfunc (c *IniConfigContainer) Set(key, val string) error {\n\tc.Lock()\n\tdefer c.Unlock()\n\tif len(key) == 0 {\n\t\treturn errors.New(\"key is empty\")\n\t}\n\n\tvar (\n\t\tsection, k string\n\t\tsectionKey = strings.Split(strings.ToLower(key), \"::\")\n\t)\n\n\tif len(sectionKey) >= 2 {\n\t\tsection = sectionKey[0]\n\t\tk = sectionKey[1]\n\t} else {\n\t\tsection = defaultSection\n\t\tk = sectionKey[0]\n\t}\n\n\tif _, ok := c.data[section]; !ok {\n\t\tc.data[section] = make(map[string]string)\n\t}","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/beego/beego/blob/939cfde380bb9f15844ad633b84f037f7da21584/core/config/ini.go#L433-L469","documentation":"IniConfigContainer.Set takes the write lock, then requires a non-empty key and rejects Set(\"\", val) immediately with \"key is empty\". Keys take the form \"key\" (stored under the default section) or \"section::key\"; the empty-string check is a cheap guard against corrupting the section map.","triggerScenarios":"Calling Set with a key built from an empty variable — e.g. Set(os.Getenv(\"CONF_KEY\"), v) when the env var is unset — or programmatic key construction that yields an empty string.","commonSituations":"Env-driven key names missing in the deployment environment; loops over key lists where one element is empty; constants or flags that resolve to empty strings at runtime.","solutions":["Validate that the key is non-empty before calling Set","Fix the source of the empty key (unset env var, empty loop element, wrong flag)","Log the offending key name at the call site for faster diagnosis","Prefer constants for key names instead of runtime construction"],"exampleFix":"// before\nkey := os.Getenv(\"CONF_KEY\") // unset -> \"\"\n_ = cfg.Set(key, \"v\") // \"key is empty\"\n\n// after\nkey := os.Getenv(\"CONF_KEY\")\nif key == \"\" {\n\treturn errors.New(\"CONF_KEY is not set\")\n}\nreturn cfg.Set(key, \"v\")","handlingStrategy":"validation","validationCode":"func mustKey(key string) (string, error) {\n\tif strings.TrimSpace(key) == \"\" {\n\t\treturn \"\", errors.New(\"config key must not be empty\")\n\t}\n\treturn key, nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never build Set keys from unvalidated env vars or loop data","Fail fast on empty identifiers at the boundary (CLI flags, env parsing)","Wrap Set in a helper that validates key shape (non-empty, single '::' separator)"],"tags":["go","beego","ini","set","empty-key"],"backgroundTag":null,"analyzedSha":"939cfde380bb9f15844ad633b84f037f7da21584","analyzedAt":"2026-08-15T17:22:06.535Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}