Documentation ¶
Overview ¶
Package dotenv supports reading and loading environment variables from .env files based on active environment (e.g. prod, dev etc.). The order or reading is as follows:
- .env
- .env.local
- .env.{active-env}
- .env.{active-env}.local
> It is recommended to not commit any .local files to the repository as these represent variables that are specific to your local environment.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶ added in v0.4.0
func Load(dir string, ae ActiveEnvironment) error
Load sets the environment variables from the active environment using env.Load.
func LoadFS ¶ added in v0.4.0
func LoadFS(fsys fs.FS, dir string, ae ActiveEnvironment) error
LoadFS sets the environment variables from the active environment using env.Load.
func Overload ¶ added in v0.4.0
func Overload(dir string, ae ActiveEnvironment) error
Overload sets and overwrites the environment variables from the active environment using env.Overload.
func OverloadFS ¶ added in v0.4.0
func OverloadFS(fsys fs.FS, dir string, ae ActiveEnvironment) error
OverloadFS sets and overwrites the environment variables from the active environment using env.Overload.
Types ¶
type ActiveEnvironment ¶ added in v0.4.0
type ActiveEnvironment string
ActiveEnvironment is the active environment.
const ( None ActiveEnvironment = "" Development ActiveEnvironment = "dev" Testing ActiveEnvironment = "test" Production ActiveEnvironment = "prod" LongFlag = "active-env" )
func GetActiveEnvironment ¶ added in v0.4.0
func GetActiveEnvironment(args []string) (ActiveEnvironment, []string)
GetActiveEnvironment returns the active environment and the remaining arguments from the provided args.
args := os.Args[1:] env, args := dotenv.GetActiveEnvironment(args...)
func GetActiveEnvironmentOr ¶ added in v0.4.0
func GetActiveEnvironmentOr(args []string, def ActiveEnvironment) (ActiveEnvironment, []string)
func (ActiveEnvironment) String ¶ added in v0.4.0
func (ae ActiveEnvironment) String() string
type NoFilesLoadedError ¶ added in v0.4.2
func (*NoFilesLoadedError) Error ¶ added in v0.4.2
func (e *NoFilesLoadedError) Error() string
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
A Reader reads .env files from a filesystem and provides the mechanism to lookup environment variables. Its zero value is ready to use and reads from the current working directory.
func Read ¶
func Read(dir string, ae ActiveEnvironment) *Reader
Read reads .env files from dir, depending on the provided ActiveEnvironment.
var cfg MyConfig dec := env.NewDecoder(dotenv.Read("./", dotenv.Development)) dec.Decode(&cfg)
Example ¶
This example reads .env files from the "example" directory and decodes the found variables into a struct.
package main import ( "embed" "github.com/davecgh/go-spew/spew" "github.com/go-pogo/env" "time" ) //go:embed example/* var fsys embed.FS // This example reads .env files from the "example" directory and decodes the // found variables into a struct. func main() { type Config struct { Foo string Timeout time.Duration `default:"10s"` } var conf Config if err := env.NewDecoder(ReadFS(fsys, "example", None)).Decode(&conf); err != nil { panic(err) } spew.Dump(conf) }
Output: (dotenv.Config) { Foo: (string) (len=3) "bar", Timeout: (time.Duration) 2s }
func ReadFS ¶ added in v0.4.0
func ReadFS(fsys fs.FS, dir string, ae ActiveEnvironment) *Reader
ReadFS reads .env files at dir from fsys, depending on the provided ActiveEnvironment.