Skip to content

digvijay-tech/envconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

envconfig

A simple Go library for loading configuration from .env files into structs using struct tags.

Installation

go get github.com/digvijay-tech/envconfig

Usage

Define a struct with envconfig tags:

type Config struct {
    Name       string  `envconfig:"NAME"`
    Age        uint8   `envconfig:"AGE"`
    IsEngineer bool    `envconfig:"IS_ENGINEER"`
    Stars      float32 `envconfig:"GITHUB_STARS"`
}

Create a .env file:

NAME=John Doe
AGE=30
IS_ENGINEER=true
GITHUB_STARS=1.5

Load the configuration:

import "github.com/digvijay-tech/envconfig"

opts := envconfig.EnvOptions[Config]{
    FileName: ".env",  // optional, defaults to ".env"
    FilePath: ".",     // optional, defaults to current directory
    Schema:   &Config{},
}

config, err := envconfig.Configure(opts)
if err != nil {
    log.Fatal(err)
}

Supported Types

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

.env File Format

  • Keys must be valid environment variable names (letters, numbers, underscores)
  • Values can be quoted with single or double quotes (optional)
  • Comments start with #
  • Empty lines are ignored
  • Invalid keys are skipped

About

A simple Go library for loading .env files into structs and environment variables.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages