Documentation
¶
Index ¶
- Variables
- func Bool(name string, value bool) *bool
- func Duration(name string, value time.Duration) *time.Duration
- func Float64(name string, value float64) *float64
- func Int(name string, value int) *int
- func Int64(name string, value int64) *int64
- func LogLevel(name string, levels map[string]int, defaultValue int) *int
- func Parse() error
- func Reset()
- func String(name string, value string) *string
- func StringSlice(name string, value []string) *[]string
- func TCPAddr(name string, value string) *string
- func ValidateTCPNetworkAddress(addr string) (*net.TCPAddr, error)
- type EnvironmentVariable
- type EnvironmentVariableSet
- func (e *EnvironmentVariableSet) Bool(name string, value bool) *bool
- func (e *EnvironmentVariableSet) BoolVar(p *bool, name string, value bool)
- func (e *EnvironmentVariableSet) Duration(name string, value time.Duration) *time.Duration
- func (e *EnvironmentVariableSet) DurationVar(p *time.Duration, name string, value time.Duration)
- func (e *EnvironmentVariableSet) Float64(name string, value float64) *float64
- func (e *EnvironmentVariableSet) Float64Var(p *float64, name string, value float64)
- func (e *EnvironmentVariableSet) Int(name string, value int) *int
- func (e *EnvironmentVariableSet) Int64(name string, value int64) *int64
- func (e *EnvironmentVariableSet) Int64Var(p *int64, name string, value int64)
- func (e *EnvironmentVariableSet) IntVar(p *int, name string, value int)
- func (e *EnvironmentVariableSet) LogLevel(name string, levels map[string]int, value int) *int
- func (e *EnvironmentVariableSet) LogLevelVar(p *int, name string, levels map[string]int, value int)
- func (e *EnvironmentVariableSet) Parse() error
- func (e *EnvironmentVariableSet) Reset()
- func (e *EnvironmentVariableSet) String(name string, value string) *string
- func (e *EnvironmentVariableSet) StringSlice(name string, value []string) *[]string
- func (e *EnvironmentVariableSet) StringSliceVar(p *[]string, name string, value []string)
- func (e *EnvironmentVariableSet) StringVar(p *string, name string, value string)
- func (e *EnvironmentVariableSet) TCPAddr(name string, value string) *string
- func (e *EnvironmentVariableSet) TCPAddrVar(p *string, name string, value string)
- func (e *EnvironmentVariableSet) Var(value Value, name string)
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEnvironmentVariableNotFound = errors.New("not found") ErrEnvironmentVariableIsEmpty = errors.New("is empty") ErrInvalid = errors.New("invalid") )
sentinel errors.
Functions ¶
func Bool ¶
Bool sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
color := getenv.Bool("COLOR", false)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*color)
}
Output: false
func Duration ¶
Duration sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"time"
"github.com/vigo/getenv"
)
func main() {
timeout := getenv.Duration("SERVER_TIMEOUT", 5*time.Second)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*timeout)
}
Output: 5s
func Float64 ¶
Float64 sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
xFactor := getenv.Float64("X_FACTOR", 1.1)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*xFactor)
}
Output: 1.1
func Int ¶
Int sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
port := getenv.Int("PORT", 8000)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*port)
}
Output: 8000
func Int64 ¶
Int64 sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
long := getenv.Int64("LONG", 9223372036854775806)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*long)
}
Output: 9223372036854775806
func LogLevel ¶ added in v0.1.0
LogLevel sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
levels := map[string]int{
"DEBUG": 0,
"INFO": 1,
"WARN": 2,
"ERROR": 3,
}
logLevel := getenv.LogLevel("LOG_LEVEL", levels, 1)
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*logLevel)
}
Output: 1
func String ¶
String sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
hmacHeader := getenv.String("HMAC_HEADER", "X-Foo-Signature")
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*hmacHeader)
}
Output: X-Foo-Signature
func StringSlice ¶ added in v0.1.0
StringSlice sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
brokers := getenv.StringSlice("BROKERS", []string{":9092", ":9093"})
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*brokers)
}
Output: [:9092 :9093]
func TCPAddr ¶
TCPAddr sets environment variable and returns the pointer of value.
Example ¶
package main
import (
"fmt"
"github.com/vigo/getenv"
)
func main() {
listen := getenv.TCPAddr("LISTEN", ":4000")
if err := getenv.Parse(); err != nil {
fmt.Println(err)
return
}
fmt.Println(*listen)
}
Output: :4000
Types ¶
type EnvironmentVariable ¶
EnvironmentVariable represents environment variable.
type EnvironmentVariableSet ¶
type EnvironmentVariableSet struct {
// contains filtered or unexported fields
}
EnvironmentVariableSet mimics flag.FlagSet type.
func (*EnvironmentVariableSet) Bool ¶
func (e *EnvironmentVariableSet) Bool(name string, value bool) *bool
Bool creates new bool.
func (*EnvironmentVariableSet) BoolVar ¶
func (e *EnvironmentVariableSet) BoolVar(p *bool, name string, value bool)
BoolVar creates new bool variable.
func (*EnvironmentVariableSet) DurationVar ¶
DurationVar creates new duration variable.
func (*EnvironmentVariableSet) Float64 ¶
func (e *EnvironmentVariableSet) Float64(name string, value float64) *float64
Float64 creates new float64.
func (*EnvironmentVariableSet) Float64Var ¶
func (e *EnvironmentVariableSet) Float64Var(p *float64, name string, value float64)
Float64Var creates new float64 variable.
func (*EnvironmentVariableSet) Int ¶
func (e *EnvironmentVariableSet) Int(name string, value int) *int
Int creates new int.
func (*EnvironmentVariableSet) Int64 ¶
func (e *EnvironmentVariableSet) Int64(name string, value int64) *int64
Int64 creates new int64.
func (*EnvironmentVariableSet) Int64Var ¶
func (e *EnvironmentVariableSet) Int64Var(p *int64, name string, value int64)
Int64Var creates new int64 variable.
func (*EnvironmentVariableSet) IntVar ¶
func (e *EnvironmentVariableSet) IntVar(p *int, name string, value int)
IntVar creates new int variable.
func (*EnvironmentVariableSet) LogLevelVar ¶ added in v0.1.0
LogLevelVar creates new log level variable.
func (*EnvironmentVariableSet) Parse ¶
func (e *EnvironmentVariableSet) Parse() error
Parse fetches environment variable, creates required Value, sets and stores.
func (*EnvironmentVariableSet) Reset ¶
func (e *EnvironmentVariableSet) Reset()
Reset resets variables storage.
func (*EnvironmentVariableSet) String ¶
func (e *EnvironmentVariableSet) String(name string, value string) *string
String creates new string.
func (*EnvironmentVariableSet) StringSlice ¶ added in v0.1.0
func (e *EnvironmentVariableSet) StringSlice(name string, value []string) *[]string
StringSlice creates new string slice.
func (*EnvironmentVariableSet) StringSliceVar ¶ added in v0.1.0
func (e *EnvironmentVariableSet) StringSliceVar(p *[]string, name string, value []string)
StringSliceVar creates new string slice variable.
func (*EnvironmentVariableSet) StringVar ¶
func (e *EnvironmentVariableSet) StringVar(p *string, name string, value string)
StringVar creates new string variable.
func (*EnvironmentVariableSet) TCPAddr ¶
func (e *EnvironmentVariableSet) TCPAddr(name string, value string) *string
TCPAddr creates new tcp addr.
func (*EnvironmentVariableSet) TCPAddrVar ¶
func (e *EnvironmentVariableSet) TCPAddrVar(p *string, name string, value string)
TCPAddrVar creates new string variable for tcp address value.
func (*EnvironmentVariableSet) Var ¶
func (e *EnvironmentVariableSet) Var(value Value, name string)
Var stores EnvironmentVariable type.