Convert Figma logo to code with AI

caarlos0 logoenv

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

4,762
245
4,762
4

Top Related Projects

26,777

Go configuration with fangs

A Go port of Ruby's dotenv library (Loads environment variables from .env files)

✨Clean and minimalistic environment configuration reader for Golang

2,645

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Quick Overview

caarlos0/env is a Go library that simplifies the process of parsing environment variables into Go structs. It provides a clean and type-safe way to handle configuration through environment variables, supporting various data types and custom parsers.

Pros

  • Easy to use with minimal boilerplate code
  • Supports a wide range of data types, including slices and maps
  • Allows for custom parsers and validators
  • Integrates well with other Go libraries and frameworks

Cons

  • Limited to environment variables as the sole configuration source
  • May require additional setup for complex nested structures
  • Doesn't provide built-in support for configuration file parsing
  • Learning curve for advanced features like custom parsers

Code Examples

  1. Basic usage:
type Config struct {
    Host string `env:"HOST" envDefault:"localhost"`
    Port int    `env:"PORT" envDefault:"8080"`
}

func main() {
    cfg := Config{}
    if err := env.Parse(&cfg); err != nil {
        fmt.Printf("%+v\n", err)
    }
    fmt.Printf("%+v\n", cfg)
}
  1. Using custom parsers:
type Config struct {
    Hosts []string `env:"HOSTS" envSeparator:":"`
}

func main() {
    cfg := Config{}
    if err := env.Parse(&cfg); err != nil {
        fmt.Printf("%+v\n", err)
    }
    fmt.Printf("%+v\n", cfg)
}
  1. Nested structs:
type Database struct {
    URL string `env:"DATABASE_URL"`
}

type Config struct {
    DB Database
}

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

Getting Started

To use caarlos0/env in your Go project, follow these steps:

  1. Install the library:

    go get github.com/caarlos0/env/v6
    
  2. Import the library in your Go code:

    import "github.com/caarlos0/env/v6"
    
  3. Define a struct with environment variable tags:

    type Config struct {
        Home         string   `env:"HOME"`
        Port         int      `env:"PORT" envDefault:"3000"`
        IsProduction bool     `env:"PRODUCTION"`
        Hosts        []string `env:"HOSTS" envSeparator:":"`
        Duration     time.Duration `env:"DURATION"`
    }
    
  4. Parse the environment variables into your struct:

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

Now you can use cfg in your application with the values populated from environment variables.

Competitor Comparisons

26,777

Go configuration with fangs

Pros of Viper

  • More comprehensive configuration management, supporting multiple config formats (JSON, YAML, TOML, etc.)
  • Supports reading from config files, environment variables, command-line flags, and remote systems
  • Offers live watching and re-reading of config files

Cons of Viper

  • More complex setup and usage compared to env's simplicity
  • Heavier dependency with more features that might not be needed for simpler projects
  • Steeper learning curve for developers new to the library

Code Comparison

env:

type Config struct {
    Home   string `env:"HOME"`
    Port   int    `env:"PORT" envDefault:"3000"`
    IsProduction bool `env:"PRODUCTION"`
}

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

Viper:

viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
    panic(fmt.Errorf("Fatal error config file: %s \n", err))
}

port := viper.GetInt("port")
production := viper.GetBool("production")

A Go port of Ruby's dotenv library (Loads environment variables from .env files)

Pros of godotenv

  • Supports loading environment variables from .env files
  • Provides a simple way to parse and set environment variables
  • Includes a CLI tool for running commands with .env file support

Cons of godotenv

  • Limited functionality for parsing complex data types
  • Lacks built-in struct tag support for automatic mapping
  • May require additional code for type conversion and validation

Code Comparison

godotenv:

import "github.com/joho/godotenv"

err := godotenv.Load()
if err != nil {
    log.Fatal("Error loading .env file")
}

env:

import "github.com/caarlos0/env/v6"

type config struct {
    Home string `env:"HOME"`
    Port int    `env:"PORT" envDefault:"3000"`
}

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

Summary

godotenv focuses on loading environment variables from .env files and provides a simple interface for parsing and setting them. It's particularly useful for projects that rely on .env files for configuration.

env, on the other hand, offers more advanced features for parsing environment variables directly into Go structs, with support for various data types, default values, and custom parsers. It's better suited for applications that require more complex configuration handling and type safety.

Choose godotenv for simpler .env file management, or env for more robust struct-based configuration parsing.

✨Clean and minimalistic environment configuration reader for Golang

Pros of cleanenv

  • Supports multiple configuration sources (environment variables, config files, flags)
  • Provides validation and custom error messages for configuration fields
  • Offers a more comprehensive set of features for complex configuration scenarios

Cons of cleanenv

  • Slightly more complex setup and usage compared to env
  • May have a steeper learning curve for beginners
  • Less focused on simplicity and minimalism

Code Comparison

env:

type Config struct {
    Home   string `env:"HOME"`
    Port   int    `env:"PORT" envDefault:"3000"`
    IsProduction bool `env:"PRODUCTION"`
}

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

cleanenv:

type Config struct {
    Home   string `env:"HOME" env-default:"/home/user"`
    Port   int    `env:"PORT" env-default:"3000"`
    IsProduction bool `env:"PRODUCTION" env-default:"false"`
}

var cfg Config
if err := cleanenv.ReadEnv(&cfg); err != nil {
    fmt.Printf("%+v\n", err)
}

Both libraries offer similar basic functionality for parsing environment variables into struct fields. However, cleanenv provides more advanced features and configuration options, while env focuses on simplicity and ease of use.

2,645

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Pros of koanf

  • Supports multiple configuration sources (environment variables, files, command-line flags)
  • Offers advanced features like watching for file changes and merging configurations
  • Provides type-safe parsing and unmarshaling of configuration data

Cons of koanf

  • More complex setup and usage compared to env's simplicity
  • Steeper learning curve due to its extensive feature set
  • Potentially overkill for simple configuration needs

Code Comparison

env:

type Config struct {
    Home   string `env:"HOME"`
    Port   int    `env:"PORT" envDefault:"3000"`
    IsProduction bool `env:"PRODUCTION"`
}

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

koanf:

var k = koanf.New(".")

k.Load(file.Provider("config.yml"), yaml.Parser())
k.Load(env.Provider("APP_", ".", func(s string) string {
    return strings.Replace(strings.ToLower(s), "_", ".", -1)
}), nil)

var config Config
if err := k.Unmarshal("", &config); err != nil {
    log.Fatalf("error unmarshalling config: %v", err)
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GoReleaser Logo

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

Getting started
type config struct {
  Home string `env:"HOME"`
}

// parse
var cfg config
err := env.Parse(&cfg)

// parse with generics
cfg, err := env.ParseAs[config]()

You can see the full documentation and list of examples at pkg.go.dev.


Used and supported by

encore icon

Encore – the platform for building Go-based cloud backends.

Usage

Caveats

[!CAUTION]

Unexported fields will be ignored by env. This is by design and will not change.

Functions

  • Parse: parse the current environment into a type
  • ParseAs: parse the current environment into a type using generics
  • ParseWithOptions: parse the current environment into a type with custom options
  • ParseAsithOptions: parse the current environment into a type with custom options and using generics
  • Must: can be used to wrap Parse.* calls to panic on error
  • GetFieldParams: get the env parsed options for a type
  • GetFieldParamsWithOptions: get the env parsed options for a type with custom options

Supported types

Out of the box all built-in types are supported, plus a few others that are commonly used.

Complete list:

  • bool
  • float32
  • float64
  • int16
  • int32
  • int64
  • int8
  • int
  • string
  • uint16
  • uint32
  • uint64
  • uint8
  • uint
  • time.Duration
  • time.Location
  • encoding.TextUnmarshaler
  • url.URL

Pointers, slices and slices of pointers, and maps of those types are also supported.

You may also add custom parsers for your types.

Tags

The following tags are provided:

  • env: sets the environment variable name and optionally takes the tag options described below
  • envDefault: sets the default value for the field
  • envPrefix: can be used in a field that is a complex type to set a prefix to all environment variables used in it
  • envSeparator: sets the character to be used to separate items in slices and maps (default: ,)
  • envKeyValSeparator: sets the character to be used to separate keys and their values in maps (default: :)

env tag options

Here are all the options available for the env tag:

  • ,expand: expands environment variables, e.g. FOO_${BAR}
  • ,file: instructs that the content of the variable is a path to a file that should be read
  • ,init: initialize nil pointers
  • ,notEmpty: make the field errors if the environment variable is empty
  • ,required: make the field errors if the environment variable is not set
  • ,unset: unset the environment variable after use

Parse Options

There are a few options available in the functions that end with WithOptions:

  • Environment: keys and values to be used instead of os.Environ()
  • TagName: specifies another tag name to use rather than the default env
  • RequiredIfNoDef: set all env fields as required if they do not declare envDefault
  • OnSet: allows to hook into the env parsing and do something when a value is set
  • Prefix: prefix to be used in all environment variables
  • UseFieldNameByDefault: defines whether or not env should use the field name by default if the env key is missing
  • FuncMap: custom parse functions for custom types

Documentation and examples

Examples are live in pkg.go.dev, and also in the example test file.

Badges

Release Software License Build status Codecov branch Go docs Powered By: GoReleaser Conventional Commits

Related projects

  • envdoc - generate documentation for environment variables from env tags

Stargazers over time

Stargazers over time