Skip to content
Node: require('dotenv').config()github.com/sahilkhaire/gox/env

Load

Overview

Load parses a dotenv file and sets variables in the environment and override layer.

If you are coming from Node.js, the closest pattern is require('dotenv').config().

Signature

go
func Load(path string) error

Compare: Node.js · Standard Go · gox

js
require('dotenv').config();
go
// use os.Getenv after loading .env manually or via a config lib
go
import "github.com/sahilkhaire/gox/env"

dir := t.TempDir()
path := filepath.Join(dir, ".env")
Set("FOO", "")
Set("ONLY_TEST", "1")
n, err := GetInt("MISSING", 7)
Set("N", "42")

Example

go
import "github.com/sahilkhaire/gox/env"

dir := t.TempDir()
path := filepath.Join(dir, ".env")
Set("FOO", "")
Set("ONLY_TEST", "1")
n, err := GetInt("MISSING", 7)
Set("N", "42")

Tips

Call Load() once at startup, then use typed getters instead of parsing strings manually.

Standard library alternative

Use the standard library directly:

go
// use os.Getenv after loading .env manually or via a config lib

Back to env package overview

MIT Licensed · Built for Node.js developers moving to Go