dotenv
.env parser and editor that keeps comments, order and quoting, plus the dotenvctl CLI.
go get github.com/ubgo/dotenv
- Language
- Go
- Go version
- 1.22
- Core deps
- 0 direct
- Latest tag
- v0.1.1
- License
- Apache-2.0
- Go modules
- 2
Code from the README
f, err := dotenv.Open(".env") // a missing file is not an error
if err != nil {
return err
}
v, ok := f.Get("DATABASE_URL")
f.Has("DATABASE_URL")
f.Keys() // active keys, in file order, deduplicated
f.Pairs() // active pairs, in file order, duplicates included
f.Map() // effective view: last occurrence winscreated := f.Set("API_KEY", "secret") // updates in place, or appends
f.Unset("OLD_KEY", false) // comments it out — reversible
f.Unset("OLD_KEY", true) // removes it outright
err := f.Save() // atomicf := dotenv.Parse("")
f.Append(
dotenv.NewComment("------------------", "DATABASE", "------------------"),
dotenv.NewPair("DB_HOST", "localhost"),
dotenv.NewPair("DB_PORT", "5432"),
dotenv.NewBlank(),
dotenv.NewComment("------------------", "AUTH", "------------------"),
dotenv.NewPair("AUTH_SECRET", "change me"),
)
f.Path = ".env.example"
f.Save()for _, e := range f.Entries() {
switch e.Kind {
case dotenv.KindPair: // active
case dotenv.KindDisabledPair: // turned off, but Key and Value are readable
case dotenv.KindComment: // prose
case dotenv.KindBlank:
case dotenv.KindOther: // unrecognised, preserved verbatim
}
}
f.Disabled() // []Pair of every commented-out setting, in file orderFrom the dotenv README.
At a glance (from the README)
| Family | Semantics | Use for |
|---|---|---|
Set, SetAfter, SetBefore | upsert — guarantees one active entry; updates in place if the key exists | editing |
Append, InsertAfter, InsertBefore | literal — places exactly what you pass, duplicates included | generating |