https://github.com/devfans/envconf
bootstrap for easily operate on config file & env
https://github.com/devfans/envconf
Last synced: 6 months ago
JSON representation
bootstrap for easily operate on config file & env
- Host: GitHub
- URL: https://github.com/devfans/envconf
- Owner: devfans
- License: gpl-3.0
- Created: 2018-06-21T08:01:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-08-11T11:25:29.000Z (8 months ago)
- Last Synced: 2025-08-11T13:13:50.343Z (8 months ago)
- Language: Go
- Size: 43.9 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# envconf
[](https://travis-ci.org/devfans/envconf)
[](https://goreportcard.com/report/github.com/devfans/envconf)
[](https://godoc.org/github.com/devfans/envconf) [](https://gitter.im/devfans/envconf?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Boostrap for operations with config file or env variables.
Read/Save config files like "~/.app" with sections and Set/Get env variables.
# Get Started
Config file sample:
```
[main]
name = test # comment
[server]
[client]
```
Usages:
```
import "github.com/devfans/envconf"
func main() {
config := envconf.NewConfig("~/.app")
// get name key from config from default section: [main]
name := config.Get("name")
// get sections
server := config.GetSection("server")
client := config.GetSection("client")
// add new keys
server.Put("ip", "localhost")
client.Put("ip", "0.0.0.0")
// save to disk as file "~/.app"
config.Save()
// other usages
config.Section = "server" // switch current section
serverIp := config.String("ip") // localhost
config.Get("SERVER_IP", "ip") // get env first if env variable is not null
config.Getenv("SERVER_IP")
config.Setenv("SERVER_IP", "localhost")
}
```
## dotenv
```
# file: .env
[main]
use_section = case1
[case1]
name = a
[case2]
name = b
```
Example
```
import (
"os"
"testing"
"github.com/devfans/envconf/dotenv"
)
func TestEnv(t *testing.T) {
t.Log(os.Getenv("a"))
t.Log(os.Getenv("b"))
t.Log(dotenv.Int("a"))
t.Log(dotenv.Uint("b"))
t.Log(dotenv.Bool("c"))
t.Log(dotenv.Bool("d"))
t.Log(os.Getenv("test"))
t.Log(dotenv.String("test"))
t.Log(dotenv.EnvConf().Get("a"))
t.Log(dotenv.EnvConf().Get("b"))
}
```