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

  1. Validate that the key is non-empty before calling Set
  2. Fix the source of the empty key (unset env var, empty loop element, wrong flag)
  3. Log the offending key name at the call site for faster diagnosis
  4. 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

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


AI-assisted analysis of beego/beego@939cfde380 (2026-08-15). Data as JSON: /api/errors/440370e8304122be. Report an issue: GitHub.