Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
func Load(ref any, options ...*EnvironmentOptions) (err error)
Load environment variables and store the result in passed ref.
Usage:
type Env struct { Key string `env:"KEY"` } var env Env err := environment.Load(&env) env.Key // Output: "value"
Example ¶
package main import ( "fmt" "github.com/stellaraf/go-utils/environment" ) func main() { type Env struct { Key string `env:"KEY"` } var env Env err := environment.Load(&env) if err != nil { panic(err) } fmt.Println(env.Key) }
Output: value
Types ¶
type EnvironmentOptions ¶
type EnvironmentOptions struct { // Environment keys and values that will be accessible for the service. From github.com/caarlos0/env/v9 Environment map[string]string // TagName specifies another tagname to use rather than the default env. From github.com/caarlos0/env/v9 TagName string // RequiredIfNoDef automatically sets all env as required if they do not // declare 'envDefault'. From github.com/caarlos0/env/v9 RequiredIfNoDef bool // OnSet allows to run a function when a value is set. From github.com/caarlos0/env/v9 OnSet func(tag string, value interface{}, isDefault bool) // Prefix define a prefix for each key. From github.com/caarlos0/env/v9 Prefix string // UseFieldNameByDefault defines whether or not env should use the field // name by default if the `env` key is missing. From github.com/caarlos0/env/v9 UseFieldNameByDefault bool // Custom parse functions for different types. From github.com/caarlos0/env/v9 FuncMap map[reflect.Type]func(v string) (interface{}, error) // DotEnv determines if environment variables should be loaded from a file named `.env`. // If `true`, `environment.Load` will look for a file named `.env` at the project root // and load its contents as environment variables. // // Default: `true` DotEnv bool // ProjectRootDepth defines the number of upward directories to check for a `.env` file // from whichever file calls `environment.Load`. // // Default: `4` ProjectRootDepth int // FileNames defines the .env file names to look for. // // Default: // []string{".env"} FileNames []string }
Click to show internal directories.
Click to hide internal directories.