https://github.com/ah-its-andy/goconf
Configuration providers for go inspired by .net core configuration libs. Extensible and easy to use.
https://github.com/ah-its-andy/goconf
config configuration go go-configuration goconf golang ini json yaml
Last synced: 4 months ago
JSON representation
Configuration providers for go inspired by .net core configuration libs. Extensible and easy to use.
- Host: GitHub
- URL: https://github.com/ah-its-andy/goconf
- Owner: ah-its-andy
- License: apache-2.0
- Created: 2022-05-10T02:42:10.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-16T07:18:31.000Z (about 4 years ago)
- Last Synced: 2024-06-20T10:17:13.821Z (almost 2 years ago)
- Topics: config, configuration, go, go-configuration, goconf, golang, ini, json, yaml
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goconf
Configuration providers for go inspired by .net core configuration libs. Extensible and easy to use.

[](https://github.com/gookit/goutil)
[](https://pkg.go.dev/github.com/ah-its-andy/goconf)
[](https://goreportcard.com/report/github.com/ah-its-andy/goconf)
[](https://github.com/ah-its-andy/goconf/actions)
## Supportives:
- Memory source
- Environment variables
- JSON file
- YAML file
- INI file
## Features:
- Multi providers support (merge sources)
- Extensible providers support (new providers can be added easily, even only one function to implement a new provider using file from local filesystem)
- Get value by full path with key delimiter (e.g. `application.bind_addr.port`)
- Cast value to specialized type with `TypeConversionFunc`
- Bind configuration section to struct
## Usage
- package: `github.com/ah-its-andy/goconf`
```go
// initialize on application startup
goconf.Init(func(b goconf.Builder) {
b.AddSource(physicalfile.Yaml(/*yaml file path, absolute or relative both supported*/)))
.AddSource(physicalfile.Json(/*json file path, absolute or relative both supported*/))
.AddSource(goconf.EnvironmentVariable(/*prefix for filter environment variables*/))
.AddSource(goconf.Memory(/*config map*/))
})
// use it anywhere
bindAddr, ok := goconf.GetString("application.bind_addr.addr") //Get string value
bindAddrWithDefault := goconf.GetStringOrDefault("application.bind_addr.addr", "default value") //returns default value when key is not found
castValue, ok := goconf.Cast("application.bind_addr.port", goconf.IntConversion) //cast value to int
castValueWithDefault := goconf.CastOrDefault("application.bind_addr.port", 0 /*default value*/, goconf.IntConversion) //cast value to int, returns default value when key is not found
section:= gocinf.GetSection("application") //get section
var application fakeStruct.Application
err := section.Bind(&application) //bind section to struct
```
## Refer
- [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) for binding struct
- [gopkg.in/yaml.v2](https://gopkg.in/yaml.v2) for yaml file support
- [github.com/stretchr/testify](https://github.com/stretchr/testify) for testing