Sling Academy
Home/Golang/Environment Variables in Go: Managing Configurations

Environment Variables in Go: Managing Configurations

Last updated: November 27, 2024

Environment variables provide a way to set configuration values for your applications without needing to modify your source code. They are invaluable in managing different configurations for various environments like development, testing, and production.

Understanding Environment Variables

Environment variables are dynamic named values which can affect the way running processes will behave on a computer. In Go, we can use environment variables to manage our configurations effectively.

Setting Environment Variables

Environment variables can be set in different ways depending on your operating system and shell. Below are examples for setting environment variables in Linux/Unix/MacOS and Windows.

Linux/Unix/MacOS

export DATABASE_URL="your_database_url_here"

Windows

set DATABASE_URL=your_database_url_here

Accessing Environment Variables in Go

In Go, the os package provides easy access to environment variables.

Here’s how you can read an environment variable:

package main

import (
  "fmt"
  "os"
)

func main() {
  databaseURL := os.Getenv("DATABASE_URL")
  if databaseURL == "" {
    fmt.Println("DATABASE_URL is not set")
  } else {
    fmt.Printf("Database URL: %s\n", databaseURL)
  }
}

Setting Environment Variables in Go

Sometimes you might need to set an environment variable through your Go application. While this is less common, Go offers a straightforward way to do this with os.Setenv.

package main

import (
  "fmt"
  "os"
)

func main() {
  err := os.Setenv("APP_MODE", "production")
  if err != nil {
    fmt.Println("Error setting environment variable")
  }
  fmt.Println("APP_MODE set to", os.Getenv("APP_MODE"))
}

Best Practices

  • Use Libraries: Consider using a library like godotenv to load environment variables from a .env file for local development.
  • Avoid Hardcoding: Do not hardcode sensitive information like API keys and URLs directly into your code.
  • Use a Configuration Package: Use a configuration management tool or package to help manage your config needs.
  • Documentation: Always document environment variables used in the application.

By effectively using environment variables in Go, you can easily manage your application configurations across multiple environments, improving the portability and security of your application.

Next Article: Deploying Go Apps to Heroku in 2 Minutes

Previous Article: Creating Minimal Docker Images for Go Apps

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant