How to: process.env in GO
Environment variables have become a standard for software development, for example popularised by the 12 factor app (3): Config to separate source code and configuration.
For local development we can utilise .env
files, in production we usually rely on secrets and other settings to be supplied by our container host.
JavaScript
In Node.js you can read the process environment variables via process.env
. Instead of using libraries like dotenv, you can as of node 22 use the --env-file
flag to pass a .env
file
MY_NAME="Jonathan"
console.log(process.env.MY_NAME)
Example of using the .env file:
node --env-file=.env index.ts
Example .env
file:
MY_NAME="Jonathan"
More process.env References
GO
In GO you can read the environment via os.Getenv
from the standard library.
MY_NAME=Jonathan go run main.go
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("MY_NAME:", os.Getenv("MY_NAME"))
}