env

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: MIT Imports: 4 Imported by: 0

README

GoDoc Build Status Go Report Card Coverage Status

Env

GoLobby Env is a simple package to read environment variable files and load them into the OS variables. It parses env files and returns their key/values as a map[string]string. It also loads or overloads them into the OS variables.

Documentation

Supported Versions

It requires Go v1.11 or newer versions.

Installation

To install this package run the following command in the root of your project

go get github.com/golobby/env
Loading

Following example demonstrates how to read and load a env file.

vs, err := env.Load(".env")
fmt.Println(vs)

The filename you pass to Load() function must be env file relative path like .env or path/to/.env. vs variable in the example will be a map[string]string] of the environment variables. It also loads the variables to OS variables.

Example

Consider this env file:

APP_NAME=MyGoApp
APP_PORT=8585

To read and load it:

vs, err := env.Load(".env")

fmt.Println(vs["APP_NAME"]) // MyGoApp
fmt.Println(vs["APP_PORT"]) // 8585
fmt.Println(os.Getenv("APP_NAME")) // MyGoApp
fmt.Println(os.Getenv("APP_PORT")) // 8585
Overloading

The function Load() will load the variables into the operating system, it ignores the variables that have already existed in the OS variables.

There is another function named Overload(). It's so similar to the Load() function but the difference is that it overwrites the OS variables.

The example below shows how it change the OS variables.

err := os.Setenv("APP_NAME", "TheOldName")
fmt.Println(os.Getenv("APP_NAME")) // TheOldName

vs, err := env.Overload(".env")
fmt.Println(os.Getenv("APP_NAME")) // MyGoApp
Syntax

This package works with standard syntax of environment variable as explained below.

Example of env file:

# This is comment

APP_NAME=MyGoAPP
APP_PORT = 8585

Rules:

  • Lines that start with # will be ignored.
  • Space around keys and values will be trimmed.
  • Empty lines will be ignored.

Contributors

License

GoLobby Env is released under the MIT License.

Documentation

Overview

Package env is a simple package to read environment variable files and load them into the OS variables. It parses env files and returns their key/values as a string map. It also loads or overloads them into the OS variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(filename string) (map[string]string, error)

Load will read the given env file and return its variables as a map, it also load variables into the OS variables. It won't overwrite the existing variables in the OS.

func Overload

func Overload(filename string) (map[string]string, error)

Overload will read the given env file and return its variables as a map, it also load variables into the OS variables. It will overwrite the existing variables in the OS.

Types

This section is empty.

Jump to

Keyboard shortcuts

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