beego/beego · error
key is empty
Error message
key is empty
What it means
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.
Source
Thrown at core/config/ini.go:451
// Put a line between sections.
if _, err = buf.WriteString(lineBreak); err != nil {
return err
}
}
}
_, err = buf.WriteTo(f)
return err
}
// Set writes a new value for key.
// if write to one section, the key need be "section::key".
// if the section is not existed, it panics.
func (c *IniConfigContainer) Set(key, val string) error {
c.Lock()
defer c.Unlock()
if len(key) == 0 {
return errors.New("key is empty")
}
var (
section, k string
sectionKey = strings.Split(strings.ToLower(key), "::")
)
if len(sectionKey) >= 2 {
section = sectionKey[0]
k = sectionKey[1]
} else {
section = defaultSection
k = sectionKey[0]
}
if _, ok := c.data[section]; !ok {
c.data[section] = make(map[string]string)
}View on GitHub (pinned to 939cfde380)
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
Example fix
// before
key := os.Getenv("CONF_KEY") // unset -> ""
_ = cfg.Set(key, "v") // "key is empty"
// after
key := os.Getenv("CONF_KEY")
if key == "" {
return errors.New("CONF_KEY is not set")
}
return cfg.Set(key, "v") Defensive patterns
Strategy: validation
Validate before calling
func mustKey(key string) (string, error) {
if strings.TrimSpace(key) == "" {
return "", errors.New("config key must not be empty")
}
return key, nil
} Prevention
- 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)
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- read the content error: "%s", should key = val
- not exist section
- key not found
- unsupported prefix params
- unsupported operation
AI-assisted analysis of beego/beego@939cfde380 (2026-08-15).
Data as JSON: /api/errors/440370e8304122be.
Report an issue: GitHub.