# README
Get ENV helper for Go
This package helps with using ENV files. It looks for an ENV file in the current working directory and any parent directories until the point it finds an ENV file or reaches the root.
Installation
1. Installation
go get github.com/wernerdweight/get-env-go
Configuration
The package needs no configuration.
Usage
By default, this package expects .env.local
file. You can choose a different file when initializing (see an example below).
Basic usage
package main
import (
"fmt"
"github.com/wernerdweight/get-env-go/getenv"
)
// let's pretend .env.local file exists and contains the following:
// APP_ENV=prod
func main() {
err := getenv.Init()
if nil != err {
// handle the error (e.g. no env file exists or not readable)
}
env, err := getenv.GetEnv("APP_ENV")
if nil != err {
// handle the error (requested env var doesn't exist)
}
fmt.Printf("APP_ENV: %s", env) // prints "prod"
}
Custom ENV file
package main
import (
"fmt"
"github.com/wernerdweight/get-env-go/getenv"
)
// let's pretend my-file.txt file exists and contains the following:
// APP_ENV=prod
func main() {
err := getenv.InitFromFile("my-file.txt")
if nil != err {
// handle the error (e.g. no env file exists or not readable)
}
env, err := getenv.GetEnv("APP_ENV")
if nil != err {
// handle the error (requested env var doesn't exist)
}
fmt.Printf("APP_ENV: %s", env) // prints "prod"
}
Errors
The following errors can occur (you can check for specific code since different errors have different severity):
package main
import (
"fmt"
"github.com/wernerdweight/get-env-go/getenv"
)
func main() {
// these are the possible err.Code values
fmt.Print(
getenv.CwdFailureError, // "can not determine current working directory"
getenv.CantAccessEnvFileError, // "error accessing an existing env file '[env-file-name]'"
getenv.NoEnvFileError, // "no [env-file-name] file exists in any of parent directories"
getenv.CantLoadEnvFileError, // "can't load [env-file-name] file"
getenv.NoSuchEnvVarError, // "non-existing ENV variable [env-var-name] requested"
)
_, err := getenv.GetEnv("APP_ENV")
if nil != err {
// would print "non-existing ENV variable [env-var-name] requested"
fmt.Print(
err.(*getenv.Error).Code,
)
}
}
License
This package is under the MIT license. See the complete license in the root directory of the bundle.