go-config

module
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2024 License: Apache-2.0

README

go-config

Simple application configuration library for Go

Installation

go get github.com/mxcd/go-config

Usage

package main

import (
	"github.com/mxcd/go-config/config"
)

func main() {
	err := config.LoadConfig([]config.Value{
		config.String("LOG_LEVEL").NotEmpty().Default("info"),
		config.Bool("DEV_MODE").Default(false),

		config.String("DB_HOST").NotEmpty().Default("localhost"),
		config.Int("DB_PORT").Default(5432),
		config.String("DB_USER").NotEmpty(),
		config.String("DB_PASSWORD").NotEmpty().Sensitive(),

		config.String("S3_HOST").NotEmpty().Default("localhost"),
		config.Int("S3_PORT").Default(9000),
		config.String("S3_ACCESS_KEY").NotEmpty(),
		config.String("S3_SECRET_KEY").NotEmpty().Sensitive(),
		config.String("S3_BUCKET").NotEmpty(),
	})

	if err != nil {
		panic(err)
	}
	config.Print()
}

The configuration struct will ensure, that required configuration values are present and that bool and int values are valid.
Sensitive values will be masked when printed:

config.Print()

Values can be accessed via the config.Get() function.

import (
	"github.com/mxcd/go-config/config"
)

func someFunction() {
  // ...
  devMode := config.Get().Bool("DEV_MODE")
	// ...
	logLevel := config.Get().String("LOG_LEVEL")
	// ...
	dbPort := config.Get().Int("DB_PORT")
  // ...
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL