Documentation ¶
Overview ¶
Deprecated: This package is no longer maintained.
Package env loads environment variables into Go types, with fallback values.
Installation ¶
Just go get the package:
go get -u github.com/TV4/env
Usage ¶
A small usage example
package main import ( "fmt" "net/url" "github.com/TV4/env" ) func main() { fmt.Println( env.Bool("BOOL", false), env.Bytes("BYTES", []byte{4, 2}), env.Duration("DURATION", 250000), env.Float64("FLOAT64", float64(2.5)), env.Int("INT", 1337), env.String("STRING", "Foobar"), env.Strings("STRINGS", []string{"Foo", "Bar"}), env.URL("URL", &url.URL{Scheme: "http", Host: "example.com"}), ) }
Index ¶
- Variables
- func Base64Bytes(key string, fallback []byte) []byte
- func Bool(key string, fallback bool) bool
- func Bytes(key string, fallback []byte) []byte
- func Duration(key string, fallback time.Duration) time.Duration
- func Float64(key string, fallback float64) float64
- func Func(v map[string]string) func(string) string
- func Int(key string, fallback int) int
- func String(key, fallback string) string
- func Strings(key string, fallback []string, seps ...string) []string
- func URL(key string, fallback *url.URL) *url.URL
- type Client
- type Map
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = NewClient(os.Getenv)
DefaultClient is the default client backed by os.Getenv
Functions ¶
func Base64Bytes ¶ added in v0.1.3
Base64Bytes returns a slice of bytes from the ENV decoded using base64.StdEncoding, or fallback variable
func Bool ¶
Bool returns a bool from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("BOOL", "t") fmt.Println(env.Bool("BOOL", false)) }
Output: true
func Bytes ¶
Bytes returns a slice of bytes from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("BYTES", "foo") fmt.Printf("%s", env.Bytes("BYTES", nil)) }
Output: foo
func Duration ¶
Duration returns a duration from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("DURATION", "23s") fmt.Printf("%s", env.Duration("DURATION", 0)) }
Output: 23s
func Float64 ¶
Float64 returns a float64 from the ENV, or a fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("FLOAT64", "1.23") fmt.Println(env.Float64("FLOAT64", 0)) }
Output: 1.23
func Int ¶
Int returns an int from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("INT", "345") fmt.Println(env.Int("INT", 0)) }
Output: 345
func String ¶
String returns a string from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("STRING", "foo bar") fmt.Println(env.String("STRING", "")) }
Output: foo bar
func Strings ¶
Strings returns a slice of strings from the ENV, or fallback variable
Example ¶
package main import ( "fmt" "os" "github.com/TV4/env" ) func main() { os.Setenv("STRINGS", "foo,bar,baz") fmt.Println(env.Strings("STRINGS", []string{})) }
Output: [foo bar baz]
func URL ¶
URL returns a URL from the ENV, or fallback URL if missing/invalid
Example ¶
package main import ( "fmt" "net/url" "os" "github.com/TV4/env" ) func main() { os.Setenv("URL", "http://example.com/foo") fmt.Println(env.URL("URL", &url.URL{Host: "fallback"}).String()) }
Output: http://example.com/foo
Types ¶
type Client ¶
type Client interface { Base64Bytes(key string, fallback []byte) []byte Bool(key string, fallback bool) bool Bytes(key string, fallback []byte) []byte Duration(key string, fallback time.Duration) time.Duration Float64(key string, fallback float64) float64 Int(key string, fallback int) int String(key, fallback string) string Strings(key string, fallback []string, seps ...string) []string URL(key string, fallback *url.URL) *url.URL }
Client is the env client interface