1# Env ![Build](https://github.com/xyproto/env/workflows/Build/badge.svg) [![GoDoc](https://godoc.org/github.com/xyproto/env?status.svg)](http://godoc.org/github.com/xyproto/env) [![Go Report Card](https://goreportcard.com/badge/github.com/xyproto/env)](https://goreportcard.com/report/github.com/xyproto/env)
2
3Makes fetching and interpreting environment variables easy and safe.
4
5Being able to provide default values when retrieving environment variables often makes program logic shorter and more readable.
6
7## Functions
8
9### func Str
10
11`func Str(name string, optionalDefault ...string) string`
12
13`Str` does the same as `os.Getenv`, but allows the user to provide a default value (optional).
14Only the first optional value is used, if the environment variable value is empty or not set.
15
16### func Bool
17
18`func Bool(envName string) bool`
19
20`Bool` returns the bool value of the given environment variable name. Returns `false` if it is not declared or empty.
21
22### func Int
23
24`func Int(envName string, defaultValue int) int`
25
26`Int` returns the number stored in the environment variable, or the given default value.
27
28### func AsBool
29
30`func AsBool(s string) bool`
31
32`AsBool` can be used to interpret a string value as either `true` or `false`. Examples of `true` values are "yes" and "1".
33
34### func Has
35
36`func Has(s string) bool`
37
38`Has` return true if the given environment variable name is non-empty.
39
40### func Int64
41
42Same as Int, but takes a default int64 value and returns an int64.
43
44### func Float64
45
46Same as Int, but takes a default float64 value and returns a float64.
47
48### DurationSeconds
49
50Takes a default int64 value, for the number of seconds, interprets the environment variable as the number of seconds and returns a `time.Duration`.
51
52### Contains
53
54Checks if the given environment variable contains the given string.
55
56### Is
57
58Checks if the given environment variable is the given value, with leading and trailing spaces trimmed before comparing both values.
59
60### HomeDir
61
62Returns the home directory of the current user, or `/tmp` if it is not available. `/home/$LOGNAME` or `/home/$USER` are returned if `$HOME` is not set.
63
64### ExpandUser
65
66Replaces `~` or `$HOME` at the start of a string with the home directory of the current user.
67
68### File
69
70Does the same as the `Str` function, but replaces a leading `~` or `$HOME` with the home directory of the current user.
71
72### Dir
73
74Does the same as the `Str` function, but replaces a leading `~` or `$HOME` with the home directory of the current user.
75
76### Path
77
78Returns the current `$PATH` as a slice of strings.
79
80## Example
81
82```go
83package main
84
85import (
86    "fmt"
87    "github.com/xyproto/env"
88)
89
90func main() {
91    fmt.Println(env.DurationSeconds("REQUEST_TIMEOUT", 1800))
92}
93```
94
95Running the above problem like this: `REQUEST_TIMEOUT=1200 ./main`, outputs:
96
97    20m0s
98
99## General info
100
101* Version: 1.7.0
102* License: MIT
103* Author: Alexander F. Rødseth <xyproto@archlinux.org>
104