env

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 3 Imported by: 0

README

Env - env to struct


A simple and zero-dependencies library to parse environment variables into structs

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Load application environment variables from a .env file into the current process.

package main

import (
	"fmt"
	"time"

	"github.com/go-zoox/env"
)

type config struct {
	Home         string         `env:"HOME"`
	Port         int            `env:"PORT,default=8080"`
	Password     string         `env:"PASSWORD,unset"`
	IsProduction bool           `env:"PRODUCTION"`
	Hosts        []string       `env:"HOSTS,separator=;"`
	TempFolder   string         `env:"TEMP_FOLDER,default=/tmp"`
  Duration     time.Duration  `env:"DURATION"`
	StringInts   map[string]int `env:"MAP_STRING_INT"`
}

func main() {
	cfg := config{}
	if err := env.Parse(&cfg); err != nil {
		fmt.Printf("%+v\n", err)
	}

  // or you can use generics
  cfg, err := env.ParseAs[config]()
  if err != nil {
		fmt.Printf("%+v\n", err)
  }

	fmt.Printf("%+v\n", cfg)
}

Inspired by

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.0.1"

Version is the current version of the package.

Functions

func Get

func Get(key string, defaultValue ...string) string

Get gets the value of the given key from system environment.

func Parse

func Parse(value any) error

Parse parses the environment into the struct of value.

Types

type DataSource added in v1.0.1

type DataSource struct {
}

DataSource is a data source that loads data from the environment.

func (DataSource) Get added in v1.0.1

func (DataSource) Get(path, key string) any

Get returns the value of the given key.

Jump to

Keyboard shortcuts

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