envconf

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

envconf

Go library for loading configuration from the environment variables.

Installation

go get github.com/mszenfeld/envconf

Getting Started

To start working with envconf define a structure for your config and use the loader:

export HOST="localhost"
export PORT="1337"
export DEBUG="true"
package main

import (
  "log"

  "github.com/mszenfeld/envconf"
)

type Config struct {
  Host  string
  Port  int
  Debug bool
}

func main() {
  var c Config

  l := envconf.NewLoader()
  if err := l.Load(&c); err != nil {
    log.Fatal(err)
  }
}

envconf also supports environment variable prefixes:

export APP_HOST="localhost"
export APP_PORT="1337"
export APP_DEBUG="true"
package main

import (
  "log"

  "github.com/mszenfeld/envconf"
)

type Config struct {
  Host  string
  Port  int
  Debug bool
}

func main() {
  var c Config

  l := envconf.NewLoader()
  l.SetPrefix("app")
  if err := l.Load(&c); err != nil {
    log.Fatal(err)
  }
}

Tags

envconf allows you to use the following tags:

  • env
  • default
  • required
type Config struct {
  Host  string `default:"localhost"`
  Port  int `required:"true"`
  Debug bool `env:"ENABLE_DEBUG" default:"false"`
}
env

This tag overrides the default environment variable name of the struct field.

default

The tag value will be used as a field's value if the environment variable associated with the field does not exist.

required

The available values for this tag are only "true" and "false". If field is marked as required and environment variable does not exist, loader will return an error. By default, fields are optional.

Supported Types

envconf has support for the following types:

  • string
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • bool

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedType = errors.New("unsupported field type")

Functions

This section is empty.

Types

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

func NewLoader

func NewLoader() *Loader

NewLoader creates a new instance of the `Loader`.

func (*Loader) Load

func (l *Loader) Load(obj any) error

Load loads values from the environment variables and pass them to the provided object.

func (*Loader) Prefix

func (l *Loader) Prefix() string

Prefix returns current `prefix` value.

func (*Loader) SetPrefix

func (l *Loader) SetPrefix(p string)

SetPrefix sets a new value for `prefix`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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