env

package
v0.0.0-...-908f275 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 3 Imported by: 5

Documentation

Overview

Package env provides utility functions for loading environment variables with default values.

A common use of the env package is for combining flag with environment variables in a Go program.

Example:

func main() {
	var (
		flProject = flag.String("http.addr", env.String("HTTP_ADDRESS", ":https"), "HTTP server address")
	)
	flag.Parse()
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string, def bool) bool

Bool returns the environment variable value specified by the key parameter, otherwise returning a default value if set.

func Int

func Int(key string, def int) int

Int returns the environment variable value specified by the key parameter, otherwise returning a default value if set.

func String

func String(key, def string) string

String returns the environment variable value specified by the key parameter, otherwise returning a default value if set.

Example
package main

import (
	"fmt"
	"os"

	"github.com/micromdm/go4/env"
)

func main() {
	addr := env.String("HTTP_ADDRESS", ":https")
	fmt.Printf("addr1: %s\n", addr)

	os.Setenv("HTTP_ADDRESS", "127.0.0.1:8080")

	addr = env.String("HTTP_ADDRESS", ":https")
	fmt.Printf("addr2: %s\n", addr)

}
Output:

addr1: :https
addr2: 127.0.0.1:8080

Types

This section is empty.

Jump to

Keyboard shortcuts

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