env

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: MIT Imports: 8 Imported by: 4

README

github.com/kovacou/go-env

Personal project.

You can automatically load your environment variable into specific struct by using the tag env:"" and calling the function env.Unmarshal().

Note: The package doesn't check the validity of the data.

➡ install

go get github.com/kovacou/go-env

➡ usage

APP_TITLE=Titre de mon application
APP_VERBOSE=true
APP_KEYS=facebook:key1,google:key2,twitter:key3
APP_SLICE_KEYS=key1,key2,key3
package main

import (
    "github.com/kovacou/go-env"
)

type Config   struct {
    Title     string            `env:"APP_TITLE"`
    Verbose   bool              `env:"APP_VERBOSE"`
    Keys      map[string]string `env:"APP_KEYS"`
    SliceKeys []string          `env:"APP_SLICE_KEYS"`
}

func main() {
    cfg := Config{}
    env.Unmarshal(&cfg)
    // Config{
    //     Title:     "My Application",
    //     Verbose:   true,
    //     Keys:      {"facebook":"key1", "google":"key2", "twitter":"key3"},
    //     SliceKeys: {"key1", "key2", "key3"}
    // }
    println(cfg.Title) // print "My Application"
}

Supported types

  • Atomic types :
    • string, *string,
    • int, *int,
    • uint, *uint,
    • int64, *int64,
    • uint64, *uint64,
    • float64, *float64,
    • bool, *bool
    • any
  • Special types :
    • time.Duration, *time.Duration
  • Maps :
    • map[string]any
    • map[string]bool
    • map[string]int
    • map[string]uint
    • map[string]int64
    • map[string]uint64
    • map[string]float64
    • map[string]string
  • Slices :
    • []any
    • []bool
    • []int
    • []uint
    • []int64
    • []uint64
    • []float64
    • []string

Documentation

Index

Constants

View Source
const NoPrefix = ""

NoPrefix is an empty string. Used by default for Unmarshal.

Variables

This section is empty.

Functions

func Format

func Format(format string, v ...any) string

Format will parse the key based on given format.

func FormatInt

func FormatInt(format string, v ...any) int

FormatInt use a format to parse a env variable as int.

func FormatInt64

func FormatInt64(format string, v ...any) int64

FormatInt64 use a format to parse a env variable as int64.

func FormatString

func FormatString(format string, v ...any) string

FormatString use a format to parse a env variable as string.

func FormatUint

func FormatUint(format string, v ...any) uint

FormatUint use a format to parse a env variable as uint.

func FormatUint64

func FormatUint64(format string, v ...any) uint64

FormatUint64 use a format to parse a env variable as uint64.

func GetInt

func GetInt(key string) int

GetInt parse a env variable as int.

func GetInt64

func GetInt64(key string) int64

GetInt64 parse a env variable as int64.

func GetString

func GetString(key string) string

GetString parse a env variable as string.

func GetUint

func GetUint(key string) uint

GetUint parse a env variable as uint.

func GetUint64

func GetUint64(key string) uint64

GetUint64 parse a env variable as uint64.

func Lookup

func Lookup(key string) (string, bool)

Lookup the env variable and trim spaces and if the key is defined.

func LookupInt

func LookupInt(key string) (int, bool)

LookupInt parse a env variable as int and if the key is defined.

func LookupInt64

func LookupInt64(key string) (int64, bool)

LookupInt64 parse a env variable as int64 and if the key is defined.

func LookupString

func LookupString(key string) (string, bool)

LookupString parse a env variable as string and if the key is defined.

func LookupUint

func LookupUint(key string) (uint, bool)

LookupUint parse a env variable as uint and if the key is defined.

func LookupUint64

func LookupUint64(key string) (uint64, bool)

LookupUint64 parse a env variable as uint64 and if the key is defined.

func Parse

func Parse(key string) string

Parse the env variable and trim spaces.

func Prefix

func Prefix(prefix, key string) string

Prefix generate a new variable name based on given prefix and key.

func PrefixInt

func PrefixInt(prefix, key string) int

PrefixInt use a prefix to parse a env variable as int.

func PrefixInt64

func PrefixInt64(prefix, key string) int64

PrefixInt64 use a prefix to parse a env variable as int64.

func PrefixString

func PrefixString(prefix, key string) string

PrefixString use a prefix to parse a env variable as string.

func PrefixUint

func PrefixUint(prefix, key string) uint

PrefixUint use a prefix to parse a env variable as uint.

func PrefixUint64

func PrefixUint64(prefix, key string) uint64

PrefixUint64 use a prefix to parse a env variable as uint64.

func Unmarshal

func Unmarshal(v any) error

Unmarshal will fill the given struct with the environment variables.

func UnmarshalWithPrefix

func UnmarshalWithPrefix(v any, prefix string) error

UnmarshalWithPrefix will fill the given struct with the environment variables prefixed with the given prefix. Only fields with tag `env:""` specified will be filled.

Types

This section is empty.

Jump to

Keyboard shortcuts

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