env

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 6 Imported by: 0

README

env

GoDoc Go codecov

A small utility package to load different types of environment variables.

Made in 🇩🇰 by maragu, maker of online Go courses.

Usage

go get github.com/maragudk/env
package main

import (
  "fmt"
  "time"

  "github.com/maragudk/env"
)

func main() {
  env.MustLoad()
  host := env.GetStringOrDefault("HOST", "localhost")
  port := env.GetIntOrDefault("PORT", 8080)
  tls := env.GetBoolOrDefault("TLS_ENABLED", false)
  shutdownTimeout := env.GetDurationOrDefault("SHUTDOWN_TIMEOUT", time.Minute)
  fmt.Println(host, port, tls, shutdownTimeout)
}

Documentation

Overview

Package env provides helpers functions to load different variable types from the environment.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBoolOrDefault

func GetBoolOrDefault(name string, defaultV bool) bool

GetBoolOrDefault value. Also returns the default value if the value is not a boolean.

func GetDurationOrDefault

func GetDurationOrDefault(name string, defaultV time.Duration) time.Duration

GetDurationOrDefault value. Also returns the default value if the value is not a time.Duration.

func GetIntOrDefault

func GetIntOrDefault(name string, defaultV int) int

GetIntOrDefault value. Also returns the default value if the value is not an int.

func GetStringOrDefault

func GetStringOrDefault(name, defaultV string) string

GetStringOrDefault value.

func Load added in v0.1.1

func Load(paths ...string) error

Load environment variables from environment files. Defaults to loading from .env.

func MustLoad added in v0.1.1

func MustLoad(paths ...string)

MustLoad calls Load and panics on errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

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