Skip to content

Env loader is a tool that parses and loads your environment variables into your struct.

License

Notifications You must be signed in to change notification settings

Shin-Thant/env-loader

Repository files navigation

Env Loader

Env-Loader is a tool that loads and parses environment variables into a struct.

Installation

go get github.com/Shin-Thant/env-loader

Motivations

If you're like me, you wouldn't like to write multiple os.LookupEnv() multiple times. Even if you have no problem with writing multiple os.LookupEnv(), you still have to parse strings into respective types.

Solution

Intorducing env-loader! Now you can just declare a struct type with the name of the environment variable you want and pass it to a function. Then magic happens. It will loads your environment variables, parses those into respective types and set them to the struct you provided.

You can load all of your environment variables in one struct or you can separate them into multiple structs. (This is my favorite feature.)

Usage Guide

  1. When your .env file is in your project root directory, you can just call like this.

    type appEnv struct {
        PORT int
    }
    envConfig := appEnv
    envloader.LoadEnv(&envConfig, nil)
  2. If your .env file is in different directory or you have different name for example, .env.development, you must specify the relative path from the root directory.

    type appEnv struct {
        PORT int
    }
    envConfig := appEnv
    envloader.LoadEnv(&envConfig, &envloader.LoadEnvOptions{
        EnvPath: "/.env.development",
    })

Docker

docker run --name envloader_test -it --rm shinthant101/env-loader:latest

About

Env loader is a tool that parses and loads your environment variables into your struct.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published