env

package module
v0.0.0-...-ce66226 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 6 Imported by: 0

README

go-env

Package env provides tools for working with environment-variables, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-env

GoDoc

Examples

Here is an example of getting the value of the environment-variable PORT as a uint16:

tcpport, found := env.Get[uint16]("PORT")

Here is an example of getting the value of the environment-variable PORT as a uint16, and it doesn't exist or isn't a uint16 number, then defaulting it to 8080:

tcpport := env.GetElse[uint16]("PORT", 8080)

Here is an example of getting the value of the environment-variable SHELL as a string:

shell, found := env.Get[string]("SHELL")

Import

To import package env use import code like the following:

import "github.com/reiver/go-env"

Installation

To install package env do the following:

GOPROXY=direct go get github.com/reiver/go-env

Author

Package env was written by Charles Iliya Krempeaux

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T Type](name string) (T, bool)

Get return the value of an environment-variable.

For example:

tcpport, found := env.Get[uint16]("PORT")

Or, for example:

shell, found := env.Get[string]("SHELL")

Note that the 2nd return value will be `false` if the environment-variable doesn't exist, or if the environment variable exists but its value is not compatible with the type.

See Type for a list of permissible types.

func GetElse

func GetElse[T Type](name string, alt T) T

GetElse return the value of an environment-variable, if it exists and whose value is parsable to the type, else it returns the `alt` value.

For example:

tcpport := env.GetElse[uint16]("PORT", 8080)

Or, for example:

shell, found := env.GetElse[string]("SHELL", "/bin/bash")

Note that the 2nd return parameter is the alternative value.

See Type for a list of permissible types.

Types

type Type

type Type interface {
	time.Duration |
		string |
		bool |
		int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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