Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
164: Practice Exercise: Building a Config File Parser
I've seen this exact bug in a dozen different code reviews over the years. Someone is tasked with writing a quick configuration parser—something to read a .env or a config.txt file—and they write a "simple" loop. It looks fine during the first test, but then it crashes the moment a developer adds a comment or a trailing space to the file.
func parseConfig(lines []string) map[string]string {
config := make(map[string]string)
for _, line := range lines {
parts := strings.Split(line, "=")
config[parts[0]] = parts[1]
}
return config
}
The Panic and the Messy Strings
There are three things happening here that will make your life miserable in production. First, if there's an empty line at the end of your file, strings.Split returns a slice with one element. When you try to access parts[1], Go will panic with an index out of range error. Your whole app just died because of a blank line.
Second, look at the keys. If your config file has PORT = 8080, your map key isn't "PORT"; it's "PORT ". You'll spend an hour debugging why config["PORT"] is returning an empty string, only to realize there's a hidden space in the key.
Finally, what happens if the value itself contains an equals sign? Imagine a connection string like DB_URL=host=localhost;port=5432. strings.Split will chop that into three pieces, and you'll lose everything after the second equals sign.
Splitting with Intent and Cleaning the Edges
To fix this, we need to be defensive. We should ignore lines that don't actually contain data, and we need to limit how many times we split the string. I always use strings.SplitN for this. By setting the limit to 2, we tell Go: "I only care about the first equals sign; leave the rest of the string alone."
Here is how I would actually write this logic:
func parseConfig(lines []string) map[string]string {
config := make(map[string]string)
for _, line := range lines {
line = strings.TrimSpace(line)
// Skip empty lines or comments
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
// You could return an error here, but skipping malformed
// lines is often more resilient for simple parsers.
continue
}
key := strings.TrimSpace(parts[0])
val := strings.TrimSpace(parts[1])
config[key] = val
}
return config
}
The strings.TrimSpace calls are the real heroes here. They ensure that whether the user wrote KEY=VAL or KEY = VAL , your application sees the exact same data. I've also added a check for #, because any config file without comments is a file that will eventually be confusing to the person maintaining it.
📋 Practical Task
Build a Robust Environment Variable Loader
Your task is to create a utility that reads a simulated configuration file (a slice of strings) and converts it into a usable Go map. Your implementation must be resilient enough to handle "dirty" input.
Requirements:
- Create a function
ParseEnv(input []string) map[string]string. - Ignore any lines that are entirely whitespace.
- Ignore any lines that start with
#(comments), even if there is leading whitespace before the#. - Trim leading and trailing whitespace from both the key and the value.
- Support values that contain equals signs (e.g.,
SECRET_KEY=abc=123=xyzshould result in a value of"abc=123=xyz"). - Skip lines that do not contain an equals sign without crashing the program.
Test your code with this input:
input := []string{
" # Database Settings ",
"DB_HOST = localhost ",
"DB_PASS = password=with=equals",
" ",
" PORT=5432",
"INVALID_LINE_NO_EQUALS",
" # End of file",
}
Your final map should contain exactly: {"DB_HOST": "localhost", "DB_PASS": "password=with=equals", "PORT": "5432"}.
There are no comments for now.