genv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2019 License: MIT Imports: 2 Imported by: 5

README

Genv Build Status GoDoc

Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

Installation

go get -u github.com/sakirsensoy/genv

Usage

Create a .env file in the root directory of your project and enter the environment variables you want to use:

# .env
APP_HOST=localhost
APP_PORT=1234
APP_DEBUG=true

In the meantime, it is optional to use the .env file. You can also send environment variables to your project in classic ways:

APP_HOST=localhost ./myproject

Rather than using your environment variables directly in your project, it is better to map and match them with a struct. Below you can see how we get our application parameters from environment variables:

// config/config.go
package config

import "github.com/sakirsensoy/genv"

type appConfig struct {
	Host string
	Port int
	Debug bool
}

var  App = &appConfig{
	Host: genv.Key("APP_HOST").String(),
	Port: genv.Key("APP_PORT").Default(8080).Int(),
	Debug: genv.Key("APP_DEBUG").Default(false).Bool(),
}

In main.go we first include the package that allows you to automatically load the environment variables from the .env file. Then we can include and use the parameters defined in config.go:

// main.go
package main

import (
	_ "github.com/sakirsensoy/genv/dotenv/autoload"

	"fmt"
	"myproject/config"
)

func  main() {
	fmt.Println(config.App.Host) // localhost
	fmt.Println(config.App.Port) // 1234
	fmt.Println(config.App.Debug) // true
}
Accessing Environment Variables

Genv provides an easy-to-use API for accessing environment variables.

First we specify the key to the variable want to access

var env = genv.Key("MY_VARIABLE")

Define default value (optional)

env = env.Default("default_value")

Finally, we specify the type of the environment variable and pass its contents to another variable

var myVariable = env.String()
Supported Variable Types

Genv provides support for the following data types:

  • String(): Returns data of String type
  • Int(): Returns data of Int32 type
  • Float(): Returns data of Float64 type
  • Bool(): Returns data of Bool type

For other types, you can use type conversion:

var stringValue = genv.Key("KEY").String()
var byteArrayValue = []byte(stringValue)

Contributing

Thanks in advance for your contributions :) I would appreciate it if you make sure that the API remains simple when developing.

code changes without tests will not be accepted

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

© Şakir Şensoy, 2019 ~ time.Now()

Released under the MIT License

Documentation

Overview

Package genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

Index

Constants

This section is empty.

Variables

View Source
var EnvVariables = make(map[string]*EnvVariable)

EnvVariables is where environment variables are stored.

Functions

This section is empty.

Types

type EnvVariable added in v1.0.1

type EnvVariable struct {
	// contains filtered or unexported fields
}

EnvVariable contains information about the environment variable, such as key, value, and default value.

func Key

func Key(key string) *EnvVariable

Key is used to determine the path of the environment variable to be accessed.

genv.Key("env-key").String()

func (*EnvVariable) Bool added in v1.0.1

func (e *EnvVariable) Bool() bool

Bool method is used for environment variables of type bool.

genv.Key("env-key").Bool()

func (*EnvVariable) Default added in v1.0.1

func (e *EnvVariable) Default(defaultValue interface{}) *EnvVariable

Default is used to specify the default value for the environment variable to be accessed.

genv.Key("env-key").Default("defaultValue").String()

func (*EnvVariable) Float added in v1.0.1

func (e *EnvVariable) Float() float64

Float method is used for environment variables of type float.

genv.Key("env-key").Float()

func (*EnvVariable) Int added in v1.0.1

func (e *EnvVariable) Int() int

Int method is used for environment variables of type int.

genv.Key("env-key").Int()

func (*EnvVariable) String added in v1.0.1

func (e *EnvVariable) String() string

String method is used for environment variables of type string.

genv.Key("env-key").String()

func (*EnvVariable) Update added in v1.0.1

func (e *EnvVariable) Update(value interface{})

Update is used to update the value of the corresponding environment variable.

genv.Key("env-key").Update("updatedValue")

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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