Categorygithub.com/mitz-it/golang-config
repositorypackage
0.0.3
Repository: https://github.com/mitz-it/golang-config.git
Documentation: pkg.go.dev

# README

Golang - Config

Quality Gate Status Coverage

Abstraction over Viper to read environment variables.

Environment variables from .env files are also supported.

Installation

  go get -u github.com/mitz-it/golang-config

Usage 1

package main

import "github.com/mitz-it/golang-config"

func main() {
  prefix := "mtz" // all environment variables starting with MTZ_ will be loaded
  start := config.StartConfig{
    ConfigPath: "../config/.env",
    Prefix: prefix,
  }

  cfg := config.NewConfig(start)

  connStr := cfg.Standard.GetString("connection_string") // MTZ_CONNECTION_STRING

  // ...
}

Usage 2

package main

import "github.com/mitz-it/golang-config"

func init() {
  prefix := "mtz" // all environment variables starting with MTZ_ will be loaded
  path := "../config/.env"
  config.LoadEnv(prefix, path)
}

func main() {
  connStr := config.Env.GetString("connection_string") // MTZ_CONNECTION_STRING

  // ...
}

Scopes

package main

import "github.com/mitz-it/golang-config"

func init() {
  config.LoadScopedEnv("scope1", "sc1", "../config/.env")
  config.LoadScopedEnv("scope1", "sc2", "../config/.env")
}

func main() {
  connStr1 := config.Scope("scope1").Env.GetString("connection_string") // SC1_CONNECTION_STRING
  connStr2 := config.Scope("scope2").Env.GetString("connection_string") // SC2_CONNECTION_STRING

  // ...
}