envldr

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 6 Imported by: 2 modules, 5 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

go-env-loader

GitHub tag (latest by date)

Load values for struct fields from environment variables defined via tags. Supports basic types, slices, maps and structs!

Quickstart

Declare environment variables:

export APP_ID='0c19d322-bc6f-43ea-8956-a853f4db9c06'
export DB_HOST='localhost'
export DB_PORT='5034'
export LOG_LEVEL='debug'
export KEY_MAP='{"success": 0, "error": 1}'
export INCLUDE='["/var/app", "/opt/mnt"]'

Declare struct types with env_var tags:

package main

import (
	"fmt"
	"github.com/y-du/go-env-loader"
)

type DatabaseConfig struct {
	Host string `env_var:"DB_HOST"`
	Port int64  `env_var:"DB_PORT"`
}

type Config struct {
	AppId      string           `env_var:"APP_ID"`
	RetryDelay int64            `env_var:"RETRY_DELAY"`
	AllowRetry bool             `env_var:"ALLOW_RETRY"`
	LogLevel   string           `env_var:"LOG_LEVEL"`
	Database   DatabaseConfig   `env_var:"DB_CONFIG"`
	KeyMap     map[string]int64 `env_var:"KEY_MAP"`
	Include    []string         `env_var:"INCLUDE"`
}

func main() {
	// declare default values
	config := Config{
		RetryDelay: 5,
		AllowRetry: true,
		LogLevel:   "info",
	}

	// load values from environment
	if err := envldr.LoadEnv(&config); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("%#v\n", config)
	// prints: main.Config{AppId:"0c19d322-bc6f-43ea-8956-a853f4db9c06", RetryDelay:5, AllowRetry:true, LogLevel:"debug", Database:main.DatabaseConfig{Host:"localhost", Port:5034}, KeyMap:map[string]int64{"error":1, "success":0}, Include:[]string{"/var/app", "/opt/mnt"}}

}

Analogous to slices and maps, struct types can also be loaded from environment:

export DB_CONFIG='{"Host": "somedb", "Port": 4021}'

The tag values DB_HOST and DB_PORT set in DatabaseConfig are now ignored:

        fmt.Printf("%#v\n", config)
        // prints: main.Config{AppId:"0c19d322-bc6f-43ea-8956-a853f4db9c06", RetryDelay:5, AllowRetry:true, LogLevel:"debug", Database:main.DatabaseConfig{Host:"somedb", Port:4021}, KeyMap:map[string]int64{"error":1, "success":0}, Include:[]string{"/var/app", "/opt/mnt"}}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEnv

func LoadEnv(itf interface{}) error

func LoadEnvUserParser

func LoadEnvUserParser(itf interface{}, keywordParsers map[string]Parser, typeParsers map[reflect.Type]Parser, kindParsers map[reflect.Kind]Parser) error

Types

type Parser

type Parser func(t reflect.Type, val string, params []string, kwParams map[string]string) (interface{}, error)

Jump to

Keyboard shortcuts

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