Configuration Management
Start with Configuration Management
Learn configuration management through an executable example, then connect the small demonstration to production code.
What you’ll learn
- Explain the main purpose of configuration management.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
Configuration changes across local development, staging and production: ports, endpoints, timeouts and feature flags. Prefer explicit typed values and validation. Use environment variables for non-secret identifiers and access sensitive values through an appropriate secret-management approach.
The sample parses a fixed configuration value for deterministic output; real startup code can read from os.LookupEnv.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
Environment-driven startup
raw, ok := os.LookupEnv("PORT")
if !ok { raw = "8080" }
config, err := parsePort(raw)
if err != nil { log.Fatal(err) }Import os and log to use this fragment in main. For production secrets, use deployment-provided secret mechanisms and restrict their exposure.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import (
"fmt"
"strconv"
)
type Config struct{ Port int }
func parsePort(raw string) (Config, error) {
port, err := strconv.Atoi(raw)
if err != nil || port < 1 || port > 65535 {
return Config{}, fmt.Errorf("invalid port")
}
return Config{Port: port}, nil
}
func main() { config, err := parsePort("8080"); fmt.Println("Config:", config, "Error:", err) }
Output
Config: {8080} Error: <nil>Trace what the program does
parsePort converts 8080 to an integer and verifies that it is a valid TCP port. The result is a typed Config with no error.
Avoid common mistakes
- Never silently accept malformed security or network configuration.
- Do not commit credentials to source, logs or example environment files.
Try it yourself
Test the values 0, 65536, abc and 9090; return useful errors while avoiding secret leaks.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Configuration Management in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.