Skip to content

A simple and flexible Go package for loading environment variables from a config file or the system environment and unmarshaling them into a struct. Automatically supports multiple data types with clear error handling.

License

Notifications You must be signed in to change notification settings

VinukaThejana/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Env

The Env package provides a generic interface for loading environment variables from a config file (such as .env) and unmarshaling them into a Go struct. If a config file is not provided, the package will attempt to load environment variables directly from the system environment. In case of an error while loading the environment variables, the program will panic with a clear indication of the error.

Features

  • Load environment variables from:

    • System environment variables
    • .env files
    • Custom file paths
  • Unmarshal environment variables into structs

  • Support for string and integer types

  • Customize configuration file name and path

  • Validation of the loaded configuration

Installation

To install go get

go get github.com/VinukaThejana/env

Usage

Here's a quick example of how to use Env

// Package env is a generic package that can be used to load environment variables 
// from a config file or system environment variables
package env

import (
    "fmt"
    environ "github.com/VinukaThejana/env"
)

// Env is a struct that contains the environment variables
type Env struct {
    DatabaseURL string `mapstructure:"DATABASE_URL"`
    Port        int    `mapstructure:"PORT"`
}

// Load loads the environment variables from the config file or system environment variables
func (e *Env)Load(path ...string) {
    environ.Load(e, path...)
}

Loading Environment Variables

Env provides several ways to load environment variables:

  1. From system environment variables if no .env file if found, Env automatically falls back to loading from the system environment variables.

  2. From .env file in the current directory By default, Env looks for a .env file in the current directory:

environ.Load(e)
  1. from custom path you can sepcify a custom path for your configuration file:
environ.Load(e, "/custom/path/to/.env/file")
  1. With custom file name You can also specify both the custom path and a custom file name:
environ.Load(e, "/custom/path/to/.env/file", "custom_file_name")

Configuring the struct

Your configuration should use the mapstructure tag to map the environment variables to the struct fields:

type Env struct {
    DatabaseURL string `mapstructure:"DATABASE_URL"`
    Port        int    `mapstructure:"PORT"`
    Debug       bool   `mapstructure:"DEBUG"`
}

Supported Types

Env currently supports the following types for struct fields:

  • string
  • int, int8, int16, int32, int64
  • float32, float64
  • bool
  • time.Time (parsed using RFC3339 format)

Error handling

Env uses the github.com/VinukaThejana/go-utils/logger package for error logging. If an error occurs during loading or parsing, it will be logged, and the program will exit.

Validation

After loading the configuration, Env automatically uses the validate tag if present in the struct and uses go-playground/validator for validating the struct fields. If there is an error the program will quit.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

Env is released under the MIT License.

Acknowledgements

This package uses the following third-party libraries:

  • github.com/spf13/viper
  • github.com/go-playground/validator
  • github.com/VinukaThejana/go-utils/logger

About

A simple and flexible Go package for loading environment variables from a config file or the system environment and unmarshaling them into a struct. Automatically supports multiple data types with clear error handling.

Topics

Resources

License

Stars

Watchers

Forks

Languages