.env · generator · Go

Go .env Generator

Go apps typically use godotenv (joho/godotenv) or envconfig (kelseyhightower/envconfig) to load .env. This generator produces a Go-idiomatic .env with PORT, DATABASE_URL, JWT_SECRET, and CORS origins.

.env · Go · 6 keys
Customize →
APP_ENV=development
PORT=8080
DATABASE_URL=postgres://user:pass@localhost:5432/app?sslmode=disable
JWT_SECRET=Ul9JW8tFiBCUnEnjS4po0VpacvMCdBdOS57TDksb7FL4p8GBTCTFeIzXkG3twnpS
LOG_LEVEL=info
CORS_ORIGINS=http://localhost:3000

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in Go

  1. 1.Save as .env in the project root.
  2. 2.Add godotenv: go get github.com/joho/godotenv.
  3. 3.Call godotenv.Load() at the very top of main(), before any other config reads.
  4. 4.Access via os.Getenv("KEY") or decode into a struct with envconfig.Process().

Gotchas

  • ⚠Call godotenv.Load() before any package-level init() that reads env — init order bites here.
  • ⚠In production, most Go hosts inject env via the kernel — don't ship .env to prod.
  • ⚠sslmode=disable is fine for dev Postgres but a hard no in prod — switch to require or verify-full.

Common keys explained

APP_ENV

development | staging | production.

PORT

HTTP bind port. 8080 is the Go default.

DATABASE_URL

Postgres DSN. Use sslmode=disable locally only.

JWT_SECRET

HMAC key for signing JWTs. Generate, don't type.

LOG_LEVEL

One of trace | debug | info | warn | error. Matches zerolog/slog.

CORS_ORIGINS

Comma-separated origins for the CORS middleware.

Loading and validating the .env in Go

config/config.go
package config

import (
	"log"

	"github.com/joho/godotenv"
	"github.com/kelseyhightower/envconfig"
)

type Config struct {
	Port        int    `envconfig:"PORT" default:"8080"`
	DatabaseURL string `envconfig:"DATABASE_URL" required:"true"`
	JWTSecret   string `envconfig:"JWT_SECRET" required:"true"`
}

func Load() Config {
	_ = godotenv.Load() // no error if .env is absent (production)
	var c Config
	if err := envconfig.Process("", &c); err != nil {
		log.Fatal(err)
	}
	return c
}

Go: env vars in development, staging and production

Go compiles to one static binary, and the usual production setup is a scratch or distroless container with no .env file at all. Call godotenv.Load() and ignore its error, so the same binary reads .env locally and real environment variables in production.

godotenv.Load() never overwrites variables that are already set. godotenv.Overload() does. Keep Load() so the orchestrator (Kubernetes, Nomad, systemd) always wins over a leftover file.

Go: .env errors and fixes

required key DATABASE_URL missing value

envconfig found no value. Make sure godotenv.Load() runs before envconfig.Process(), and that the binary is started from the directory that holds .env.

open .env: no such file or directory

Expected in production. Ignore the error from godotenv.Load(), or only log it in development.

strconv.Atoi: parsing "": invalid syntax

An int field got an empty string. Add a default tag or required:"true" so the error message names the variable.

Go .env FAQ

Do I need godotenv in Go?

Only for local development. os.Getenv reads real environment variables. godotenv just fills them from a file first.

envconfig, caarlos0/env or viper?

envconfig and caarlos0/env map env vars onto a struct with tags and are lightweight. Viper also reads YAML/JSON/TOML and remote config, which is more than most services need.

How do I pass env vars to a Go binary in Docker?

Use docker run --env-file .env, or the environment/env_file keys in Compose. The binary needs no dotenv logic for that.

Related tools

Other stacks