dotenv

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

README

dotenv

A lightweight, zero-dependency Go package for loading environment variables from .env and .env.local files into your application.

Features

  • File Priority: Automatically loads .env and overrides with .env.local if present.
  • Shell Support: Recognizes the export keyword.
  • Comment Handling: Ignores lines starting with # and strips inline comments.
  • Smart Quoting: Automatically handles values wrapped in single (') or double (") quotes.
  • Zero Dependencies: Uses only the Go standard library.

Installation

go get github.com/rickferrdev/dotenv

Usage

1. Create your environment files

.env

PORT=8080
DB_URL="postgres://user:password@localhost:5432/db"
# This is a comment
DEBUG=true

.env.local

PORT=3000 # Overrides the value in .env

2. Load variables in Go

Call dotenv.Collect() as early as possible in your main function (or in an init function) to populate os.Environ.

package main

import (
    "fmt"
    "os"
    "github.com/rickferrdev/dotenv"
)

func main() {
    // Load variables from .env and .env.local
    dotenv.Collect()

    port := os.Getenv("PORT")
    fmt.Printf("Server starting on port: %s\n", port)
}

How it Works

  • Collect(): Iterates through FilenameVariables (defaulting to .env and .env.local). It parses each line, strips the export prefix if it exists, and ignores comments.
  • quotes(): A helper function that ensures values like KEY="value" or KEY='value' are stored simply as value, while also cleaning up trailing inline comments.

Configuration

You can override the files the package looks for by modifying the FilenameVariables slice before calling Collect:

dotenv.FilenameVariables = []string{".env.production", ".env"}
dotenv.Collect()

Documentation

Overview

Package dotenv provides simple utilities to load environment variables from local files.

quotes processes a raw string value from an environment variable line.

Index

Constants

This section is empty.

Variables

View Source
var FilenameVariables = []string{".env", ".env.local"}

FilenameVariables defines the default files the package searches for.

Functions

func Collect

func Collect()

Collect iterates through the predefined filenames in FilenameVariables, parses their content, and sets the resulting key-value pairs as environment variables in the current process.

It supports:

  • Standard KEY=VALUE pairs.
  • Lines starting with "export ".
  • Comments starting with "#".
  • Basic handling of quoted values (via the internal quotes function

Types

This section is empty.

Directories

Path Synopsis
Package auto provides a side-effect import to automatically load environment variables from .env files during application initialization.
Package auto provides a side-effect import to automatically load environment variables from .env files during application initialization.

Jump to

Keyboard shortcuts

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