Skip to content

Immutable configuration loaded from environment variables. Automatically load environmental variables into structs with private properties.

License

Notifications You must be signed in to change notification settings

J7mbo/GoEnvConfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoEnvConfig

Immutable configuration loaded from environment variables.

Build Status codecov GoDoc Version License: MIT

Automatically load environmental variables into structs with private properties.

Installation

go get github.com/j7mbo/goenvconfig

Example

Bash:

export PORT=1337 

Go:

package main

import (
    "github.com/j7mbo/goenvconfig"
    "fmt"
)

type Config struct {
    host     string  `env:"HOME" default:"localhost"`
    port     int     `env:"PORT" default:"8080"`
}

func (c *Config) GetHost() string { return c.host }
func (c *Config) GetPort() int { return c.port }

func main() {
    config := Config{}
    parser := goenvconfig.NewGoEnvParser()
    
    if err := parser.Parse(&config) {
    	panic(err)
    }
    
    fmt.Println(config.GetHost()) // localhost
    fmt.Println(config.GetPort()) // 1337
}

Supported Types

For now the following simple types are supported:

  • int
  • string

Why

Just because you want to automatically load environment variables into configuration structs does not mean you should expose modifiable exported properties on your configuration object. Instead the struct should be immutable with properties only accessible via getters.

You can either idiomatically create a factory method thereby greatly reducing the simplicity of an automated solution, or you do something you're "not supposed to" and use a library that utilises the reflect and unsafe packages.

About

Immutable configuration loaded from environment variables. Automatically load environmental variables into structs with private properties.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages