Giter VIP home page Giter VIP logo

talut / dotenv Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 15 KB

Go library for managing environment variables. It provides a set of functions to retrieve environment variables with different types, including string, boolean, integer, float, and duration. It also includes "Must" versions of these functions that panic if the environment variable is not set or cannot be parsed correctly.

License: MIT License

Go 100.00%

dotenv's Introduction

dotenv

Go Reference Go Report Card License codecov

dotenv Go library for managing environment variables. It provides a set of functions to retrieve environment variables with different types, including string, boolean, integer, float, and duration. It also includes "Must" versions of these functions that panic if the environment variable is not set or cannot be parsed correctly.

Functions

  • ClearCache(): Clears the cache of environment variables.
  • GetString(key, fallback string) string: Retrieves the value of the environment variable named by the key. If the value is not set, the fallback value is returned.
  • GetBool(key string, fallback bool) bool: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a boolean, the fallback value is returned.
  • GetInt(key string, fallback int) int: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as an integer, the fallback value is returned.
  • GetFloat(key string, fallback float64) float64: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a float, the fallback value is returned.
  • GetDuration(key string, fallback time.Duration) time.Duration: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a duration, the fallback value is returned.
  • MustGetString(key string) string: Retrieves the value of the environment variable named by the key. If the value is not set, a panic occurs.
  • MustGetBool(key string) bool: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a boolean, a panic occurs.
  • MustGetInt(key string) int: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as an integer, a panic occurs.
  • MustGetFloat(key string) float64: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a float, a panic occurs.
  • MustGetDuration(key string) time.Duration: Retrieves the value of the environment variable named by the key. If the value is not set or cannot be parsed as a duration, a panic occurs.

Installation

go get github.com/talut/dotenv

Usage

Import the import "github.com/talut/dotenv" package into your Go code using the following import statement:

Mote: The second parameters in the Get functions are fallback values that will be used if the environment variable is not set. The MustGet functions will panic if the environment variable is not set or cannot be parsed correctly.

package main

import (
	"fmt"
	"time"

	"github.com/talut/dotenv"
)

func main() {
	// Example usage of GetString
	fmt.Println(dotenv.GetString("STRING_ENV_VAR", "default"))

	// Example usage of GetBool
	fmt.Println(dotenv.GetBool("BOOL_ENV_VAR", false))

	// Example usage of GetInt
	fmt.Println(dotenv.GetInt("INT_ENV_VAR", 0))

	// Example usage of GetFloat
	fmt.Println(dotenv.GetFloat("FLOAT_ENV_VAR", 0.0))

	// Example usage of GetDuration
	fmt.Println(dotenv.GetDuration("DURATION_ENV_VAR", 1*time.Second))

	// Example usage of MustGetString
	fmt.Println(dotenv.MustGetString("MUST_STRING_ENV_VAR"))

	// Example usage of MustGetBool
	fmt.Println(dotenv.MustGetBool("MUST_BOOL_ENV_VAR"))

	// Example usage of MustGetInt
	fmt.Println(dotenv.MustGetInt("MUST_INT_ENV_VAR"))

	// Example usage of MustGetFloat
	fmt.Println(dotenv.MustGetFloat("MUST_FLOAT_ENV_VAR"))

	// Example usage of MustGetDuration
	fmt.Println(dotenv.MustGetDuration("MUST_DURATION_ENV_VAR"))

	// Example usage of ClearCache
	dotenv.ClearCache()
}

Please replace "STRING_ENV_VAR", "BOOL_ENV_VAR", "INT_ENV_VAR", "FLOAT_ENV_VAR", "DURATION_ENV_VAR", " MUST_STRING_ENV_VAR", "MUST_BOOL_ENV_VAR", "MUST_INT_ENV_VAR", "MUST_FLOAT_ENV_VAR", and "MUST_DURATION_ENV_VAR" with your actual environment variable names.

License

This project is licensed under the MIT License - see the LICENSE file for details.

dotenv's People

Contributors

talut avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.