← All Exercises
Parse a Sectioned INI Config
intermediatego
Background
Small configuration files are still user input; precise diagnostics make misconfiguration obvious instead of silently dropping settings.
Problem
Implement parseConfig(text) (map[string]map[string]string, error). Support [section] headers, key=value entries, blank lines, and # or ; comments. Reject keys outside a section, empty keys or section names, duplicate sections, duplicate keys, and malformed lines, always naming the 1-based line number.
Input
INI text as one string.
Output
A nested map of section to key/value strings, or a line-numbered error.
Sample tests
Sample 1
Input: [db]\nhost = localhost\nport = 5432\n\n# cache\n[app]\nname = demo\n
Expected: app.name=demo, db.host=localhost, db.port=5432
Sample 2
Input: host=localhost\n
Expected: line 1: key outside a section
Sample 3
Input: [db]\nport=5432\nport=5433\n
Expected: line 3: duplicate key "port"
Your solution
Learning objective
Build a tolerant-but-strict text parser with actionable diagnostics and deterministic output.