This directory contains practical examples demonstrating how to use the gitconfig library.
1. Basic Read
Demonstrates reading configuration values from git config using the simple Config API.
Topics:
- Loading a config file
- Reading string values
- Handling missing keys
Run:
go run -tags=examples examples/01-basic-read.goShows how to modify configuration values and persist changes back to disk.
Topics:
- Setting configuration values
- Persistence to file
- Verifying changes on disk
Run:
go run -tags=examples examples/02-write-persist.goDemonstrates the configuration scope hierarchy and how gitconfig resolves values across scopes.
Topics:
- System-wide config
- User config
- Local repository config
- Environment variables
- Scope priority/precedence
Run:
go run -tags=examples examples/03-scopes.go4. Custom Paths
Shows how to work with custom configuration file paths instead of default Git locations.
Topics:
- Custom config paths
- Non-standard locations
- Loading from arbitrary files
Run:
go run -tags=examples examples/04-custom-paths.goDemonstrates proper error handling patterns when working with gitconfig.
Topics:
- Parse errors
- File not found
- Permission errors
- Invalid key formats
Run:
go run -tags=examples examples/05-error-handling.goShows how gitconfig handles include directives for modular configuration.
Topics:
- Including other config files
- Conditional includes
- External config organization
Run:
go run -tags=examples examples/06-includes.goBefore running these examples, ensure you have Go 1.24 or later installed:
go version- Each example is a standalone Go file
- Run with
go run -tags=examples examples/<number>-<name>.go - Some examples create temporary files for demonstration
- Review the source code to understand each pattern
- Modify and experiment to learn more
Recommended order for learning:
- Start with 01-basic-read to understand basic usage
- Move to 02-write-persist to learn mutation
- Learn scope hierarchy with 03-scopes
- Explore flexibility with 04-custom-paths
- Master error handling with 05-error-handling
- Build modular configs with 06-includes
cfg, _ := gitconfig.NewConfig("path/to/.git/config")
value, ok := cfg.Get("user.name")
if ok {
fmt.Println("Name:", value)
}cfg, _ := gitconfig.NewConfig("path/to/.git/config")
cfg.Set("core.editor", "vim")
_ = cfg.String() // Persist changescfg, _ := gitconfig.NewConfigs() // Load all scopes
value := cfg.Get("user.name") // Respects scope priority
cfg.SetLocal("core.pager", "less") // Write to local only- README - Overview and quick start
- ARCHITECTURE - Design and structure
- CONTRIBUTING - How to contribute
- godoc - API documentation