env
API
env
packageAPI reference for the env
package.
Imports
(3)
S
struct
Provider
Provider implements configuration.Provider for environment variables.
pkg/configuration/source/env/provider.go:15-18
type Provider struct
Example
p := &env.Provider{Prefix: "APP_"}
data, err := p.Load(ctx)
Methods
Load
Method
Load reads all environment variables, filtering by prefix if set.
Parameters
ctx
context.Context
Returns
map[string]any
error
func (*Provider) Load(ctx context.Context) (map[string]any, error)
{
data := make(map[string]any)
env := os.Environ()
for _, kv := range env {
k, v, ok := strings.Cut(kv, "=")
if !ok {
continue
}
if p.Prefix != "" {
if strings.HasPrefix(k, p.Prefix) {
key := strings.TrimPrefix(k, p.Prefix)
if strings.HasSuffix(p.Prefix, "_") {
// Prefix include underscore
} else if strings.HasPrefix(key, "_") {
key = strings.TrimPrefix(key, "_")
}
data[strings.ToLower(key)] = v
}
} else {
data[strings.ToLower(k)] = v
}
}
return data, nil
}
Fields
| Name | Type | Description |
|---|---|---|
| Prefix | string |
F
function
New
New creates a new environment variable provider with an optional prefix.
Parameters
prefix
string
Returns
pkg/configuration/source/env/provider.go:21-23
func New(prefix string) *Provider
{
return &Provider{Prefix: prefix}
}