Skip to content
/ mura Public

An environment variable reader for Go that bind env value into struct variable, with zero external dependency.

License

Notifications You must be signed in to change notification settings

rasatmaja/mura

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🐍 Mura

An environment variable reader for Go that bind env value into struct variable, with zero external dependency. The main idea of ​​making this project is the use of os and reflect packages from Go.

Overview

This package will provide following features:

  • Read system environtment variable bassed on env struct tag
  • Bind env value into struct
  • Fill struct with default value if env not present

Installation

To install this package we can run go command:

go get -u github.com/rasatmaja/mura/v2

Usage

import "github.com/rasatmaja/mura/v2"

type Config struct {
    Host  string `env:"SERVER_HOST" default:"localhost"`
    Port  int    `env:"SERVER_PORT" default:"8080"`
    TLS   bool   `env:"SERVER_TLS" default:"false"`
}

cfg := new(Config)

err := mura.Unmarshal(cfg)
if err != nil {
    panic(err)
}

The code above will do:

  1. Read environtment variable based on env tag defined in struct
  2. If the environment variable is found, it will fill the struct value with env value
  3. If no environment variable found, the struct field will be filled with default tag's value

About

An environment variable reader for Go that bind env value into struct variable, with zero external dependency.

Topics

Resources

License

Stars

Watchers

Forks

Languages