https://github.com/hop-/goconfig
A Go port of Node.js config package (which uses json files to configure application)
https://github.com/hop-/goconfig
config configuration environment environment-variables go golang json json-configuration node-config
Last synced: 2 months ago
JSON representation
A Go port of Node.js config package (which uses json files to configure application)
- Host: GitHub
- URL: https://github.com/hop-/goconfig
- Owner: hop-
- License: mit
- Created: 2020-03-12T12:10:43.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-01T22:07:01.000Z (over 1 year ago)
- Last Synced: 2024-08-08T09:59:17.227Z (over 1 year ago)
- Topics: config, configuration, environment, environment-variables, go, golang, json, json-configuration, node-config
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoConfig
 [](https://goreportcard.com/report/github.com/hop-/goconfig)[](https://pkg.go.dev/github.com/hop-/goconfig)
A Go port of Node.js config package (which uses json files to configure application)
From original Library:
> Library organizes hierarchical configurations for your app deployments.
> It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).
## Installation
As a library
```shell
go get github.com/hop-/goconfig
```
Usage:
All config files are in `HOST_CONFIG_DIR` directory, default is 'config'
It is using `HOST_ENV` environment variable to define the application deployment environment
`$HOST_CONFIG_DIR`/default.json:
```json
{
"Customer": {
"db": {
"host": "localhost",
"port": 27017,
"dbName": "customers"
},
"credit": {
"initialLimit": 100,
"initialDays": 1
}
}
}
```
Override some configurations for production when `HOST_ENV` is 'production'.
`$HOST_CONFIG_DIR`/production.json:
```json
{
"Customer": {
"credit": {
"initialDays": 30
}
}
}
```
Use config in your code:
```go
import "github.com/hop-/goconfig"
type Consumer struct {
Consumer struct {
Db struct {
host string
port int
dbName string
}
Credit struct {
InitialLimit int
InitialDays int
}
}
}
func main() {
if err := goconfig.Load(); err != nil {
// Some error handling
}
consumer, err := goconfig.Get[Consumer]("Consumer")
if err != nil {
// Some error handling
}
host, err := goconfig.Get[string]("Consumer.Db.host")
if err != nil {
// Some error handling
}
// Your code
}
```