env

package module
v2.2.4 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2023 License: MIT Imports: 6 Imported by: 2

README

GoDoc CI CodeQL Go Report Card Coverage Status

Env

GoLobby Env is a lightweight package for loading OS environment variables into structs for Go projects.

Documentation

Supported Versions

It requires Go v1.16 or newer versions.

Installation

To install this package run the following command in the root of your project

go get github.com/golobby/env/v2
Usage Example

The following example demonstrates how to use GoLobby Env package.

type Config struct {
    Debug bool      `env:"DEBUG"` // Possible Values: "true", "false", "1", "0"
    App struct {
        Name string `env:"APP_NAME"`
        Port int16  `env:"APP_PORT"`
    }
    Database struct {
        Name string `env:"DB_NAME"`
        Port int16  `env:"DB_PORT"`
        User string `env:"DB_USER"`
        Pass string `env:"DB_PASS"`
    }
    IPs []string `env:IPS` // Possible Value: "192.168.0.1, 192.168.0.2"
    IDs []int32  `env:IDS` // Possible Value: "10, 11, 12"
}

config := Config{}
err := env.Feed(&config)

// Use `config` struct in your app!
Usage Tips
  • The Feed() function gets a pointer of a struct.
  • It ignores empty OS environment variables.
  • It supports nested structs and struct pointers.
Field Types

GoLobby Env uses the GoLobby Cast package to cast OS environment variables to related struct field types. Here you can see the supported types:

https://github.com/golobby/cast#supported-types

See Also

  • GoLobby/Config: A lightweight yet powerful configuration management for Go projects
  • GoLobby/DotEnv: A lightweight package for loading dot env (.env) files into structs for Go projects

License

GoLobby Env is released under the MIT License.

Documentation

Overview

Package env is a lightweight library for loading OS environment variables into structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Feed added in v2.1.0

func Feed(structure interface{}) error

Feed loads OS environment variables into the given struct. It gets the pointer of a struct that is going to holds the variables. The struct fields must have an `env` tag that determines the related OS environment variable. `env:"APP_NAME"` is a tag example.

Types

This section is empty.

Jump to

Keyboard shortcuts

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