envloader

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2024 License: Unlicense Imports: 7 Imported by: 12

README

EnvLoader

GitHub Go Reference

EnvLoader is a Go package that loads environment variables from a .env file and optionally binds them to a struct.

Installation

go get gopkg.eu.org/envloader

Usage

Loading Environment Variables
package main

import (
    "fmt"

    "gopkg.eu.org/envloader"
)

func main() {
    // Load environment variables from a file.
    err := envloader.LoadEnvFile(".env")
    if err != nil {
        panic(err)
    }

    // Access the environment variables.
    fmt.Println(os.Getenv("API_KEY"))
    fmt.Println(os.Getenv("DATABASE_URL"))
}
Binding Environment Variables to a Struct
package main

import (
    "fmt"

    "gopkg.eu.org/envloader"
)

type Config struct {
    APIKey     string `env:"API_KEY"`
    DatabaseURL string `env:"DATABASE_URL"`
}

func main() {
    // Create a config struct.
    var config Config

    // Load and bind environment variables from a file.
    err := envloader.LoadAndBindEnvFile(".env", &config)
    if err != nil {
        panic(err)
    }

    // Access the config values.
    fmt.Println(config.APIKey)
    fmt.Println(config.DatabaseURL)
}

Struct Tag Options

You can use the env struct tag to specify the environment variable name to bind to a struct field:

type Config struct {
    APIKey     string `env:"API_KEY"`
    DatabaseURL string `env:"DATABASE_URL"`
}

Additionally, you can mark a field as required by adding the required option to the env tag. This will raise an error if the environment variable is not set:

type Config struct {
    APIKey     string `env:"API_KEY,required"`
    DatabaseURL string `env:"DATABASE_URL,required"`
}

Contributing

Contributions are welcome! Please open an issue or pull request if you have any suggestions or bug reports.

License

This package is licensed under the Unlicense.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindStruct

func BindStruct(config interface{}, provider dataProvider) error

BindStruct binds data from a provider to a struct's fields using reflection.

func LoadAndBindEnvFile

func LoadAndBindEnvFile(path string, dst interface{}) error

LoadAndBindEnvFile loads environment variables from a file at the given path and binds them to a struct.

func LoadAndBindEnvReader

func LoadAndBindEnvReader(r io.Reader, dst interface{}) error

LoadAndBindEnvReader loads environment variables from a reader and binds them to a struct.

func LoadEnvFile

func LoadEnvFile(path string) error

LoadEnvFile loads environment variables from a file at the given path.

func LoadEnvReader

func LoadEnvReader(r io.Reader) error

LoadEnvReader loads environment variables from a reader (e.g., a file) and sets them as environment variables.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL