README

Extensional OS environment variables helper for Go.

Build Status Coverage Status Go.Dev reference Go Report Card Release Downloads

Usage

$ go get -u clevergo.tech/osenv
Get

Gets the environment variable associated with the given key, a fallback(default) value will be returned if not exist.

Assume environment is that FOO=BAR EMPTY=.

Method Value
osenv.Get("FOO") BAR
osenv.Get("EMPTY") -
osenv.Get("EMPTY", "BUZZ") -
osenv.Get("FIZZ") -
osenv.Get("FIZZ", "BUZZ") BUZZ

Similar functions:

  • GetInt
MustGet

Gets the environment variable associated with the given key, and panics if not exist.

osenv.MustGet("FOO") // BAR
osenv.MustGet("NIL") // panics

Similar functions:

  • MustGetInt
SetNX

Sets an environment variable if not exist.

osenv.SetNX("NONEXISTENT", "VALUE")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string, fallback ...string) string

    Get returns the environment variable associated with the given key. Returns the fallback value if not exists.

    func GetInt

    func GetInt(key string, fallback ...int) (int, error)

      GetInt converts the environment variable associated with the given key to int and returns it. Returns the fallback value if the key is not exists or the value is invalid.

      func MustGet

      func MustGet(key string) string

        MustGet returns the environment variable associated with the given key, and panics if the key is not exists.

        func MustGetInt

        func MustGetInt(key string) int

          MustGetInt converts the environment variable associated with the given key to int and returns it. Panics if the key is not exists or the value is invalid.

          func SetNX

          func SetNX(key, value string) error

            SetNX sets the value of the environment variable named by the key if not exist.

            Types

            This section is empty.

            Source Files