gotenv alternatives and similar packages
Based on the "Utilities" category.
Alternatively, view gotenv alternatives based on common mentions on social networks and blogs.
-
项目文档
基于vite+vue3+gin搭建的开发基础平台(支持TS,JS混用),集成jwt鉴权,权限管理,动态路由,显隐可控组件,分页封装,多点登录拦截,资源权限,上传下载,代码生成器,表单生成器,chatGPT自动查表等开发必备功能。 -
excelize
Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets -
godotenv
A Go port of Ruby's dotenv library (Loads environment variables from .env files) -
xlsx
(No longer maintained!) Go (golang) library for reading and writing XLSX files. -
hystrix-go
Netflix's Hystrix latency and fault tolerance library, for Go -
go-funk
A modern Go utility library which provides helpers (map, find, contains, filter, ...) -
Kopia
Cross-platform backup tool for Windows, macOS & Linux with fast, incremental backups, client-side end-to-end encryption, compression and data deduplication. CLI and GUI included. -
gorequest
GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent ) -
goreporter
A Golang tool that does static analysis, unit testing, code review and generate code quality report. -
gojson
Automatically generate Go (golang) struct definitions from example JSON -
lancet
A comprehensive, efficient, and reusable util function library of go. -
create-go-app
✨ Create a new production-ready project with backend, frontend and deploy automation by running one CLI command! -
spinner
Go (golang) package with 90 configurable terminal spinner/progress indicators. -
EaseProbe
A simple, standalone, and lightweight tool that can do health/status checking, written in Go. -
filetype
Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature -
mole
CLI application to create ssh tunnels focused on resiliency and user experience. -
boilr
:zap: boilerplate template manager that generates files or directories from template repositories -
beaver
💨 A real time messaging system to build a scalable in-app notifications, multiplayer games, chat apps in web and mobile apps. -
mimetype
A fast Golang library for media type and file extension detection, based on magic numbers -
go-underscore
Helpfully Functional Go - A useful collection of Go utilities. Designed for programmer happiness. -
JobRunner
Framework for performing work asynchronously, outside of the request flow -
git-time-metric
Simple, seamless, lightweight time tracking for Git
Access the most powerful time series database as a service
Do you think we are missing an alternative of gotenv or a related project?
Popular Comparisons
README
gotenv
Load environment variables from .env
or io.Reader
in Go.
Usage
Put the gotenv package on your import
statement:
import "github.com/subosito/gotenv"
To modify your app environment variables, gotenv
expose 2 main functions:
gotenv.Load
gotenv.Apply
By default, gotenv.Load
will look for a file called .env
in the current working directory.
Behind the scene, it will then load .env
file and export the valid variables to the environment variables. Make sure you call the method as soon as possible to ensure it loads all variables, say, put it on init()
function.
Once loaded you can use os.Getenv()
to get the value of the variable.
Let's say you have .env
file:
APP_ID=1234567
APP_SECRET=abcdef
Here's the example of your app:
package main
import (
"github.com/subosito/gotenv"
"log"
"os"
)
func init() {
gotenv.Load()
}
func main() {
log.Println(os.Getenv("APP_ID")) // "1234567"
log.Println(os.Getenv("APP_SECRET")) // "abcdef"
}
You can also load other than .env
file if you wish. Just supply filenames when calling Load()
. It will load them in order and the first value set for a variable will win.:
gotenv.Load(".env.production", "credentials")
While gotenv.Load
loads entries from .env
file, gotenv.Apply
allows you to use any io.Reader
:
gotenv.Apply(strings.NewReader("APP_ID=1234567"))
log.Println(os.Getenv("APP_ID"))
// Output: "1234567"
Both gotenv.Load
and gotenv.Apply
DO NOT overrides existing environment variables. If you want to override existing ones, you can see section below.
Environment Overrides
Besides above functions, gotenv
also provides another functions that overrides existing:
gotenv.OverLoad
gotenv.OverApply
Here's the example of this overrides behavior:
os.Setenv("HELLO", "world")
// NOTE: using Apply existing value will be reserved
gotenv.Apply(strings.NewReader("HELLO=universe"))
fmt.Println(os.Getenv("HELLO"))
// Output: "world"
// NOTE: using OverApply existing value will be overridden
gotenv.OverApply(strings.NewReader("HELLO=universe"))
fmt.Println(os.Getenv("HELLO"))
// Output: "universe"
Throw a Panic
Both gotenv.Load
and gotenv.OverLoad
returns an error on something wrong occurred, like your env file is not exist, and so on. To make it easier to use, gotenv
also provides gotenv.Must
helper, to let it panic when an error returned.
err := gotenv.Load(".env-is-not-exist")
fmt.Println("error", err)
// error: open .env-is-not-exist: no such file or directory
gotenv.Must(gotenv.Load, ".env-is-not-exist")
// it will throw a panic
// panic: open .env-is-not-exist: no such file or directory
Another Scenario
Just in case you want to parse environment variables from any io.Reader
, gotenv keeps its Parse
and StrictParse
function as public API so you can use that.
// import "strings"
pairs := gotenv.Parse(strings.NewReader("FOO=test\nBAR=$FOO"))
// gotenv.Env{"FOO": "test", "BAR": "test"}
pairs, err := gotenv.StrictParse(strings.NewReader(`FOO="bar"`))
// gotenv.Env{"FOO": "bar"}
Parse
ignores invalid lines and returns Env
of valid environment variables, while StrictParse
returns an error for invalid lines.
Notes
The gotenv package is a Go port of dotenv
project with some additions made for Go. For general features, it aims to be compatible as close as possible.