Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Environment Variables

This Go program demonstrates how to work with environment variables using the os package. It sets the value of the "FOO" environment variable, retrieves its value, and prints all environment variables. Let's break down the code with inline comments:

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	// Set the value of the "FOO" environment variable to "1"
	os.Setenv("FOO", "1")

	// Retrieve and print the value of the "FOO" environment variable
	fmt.Println("FOO:", os.Getenv("FOO"))

	// Retrieve and print the value of the "BAR" environment variable (not set)
	fmt.Println("BAR:", os.Getenv("BAR"))

	fmt.Println()

	// Iterate through all environment variables and print their names
	for _, e := range os.Environ() {
		// Split the environment variable into a key-value pair
		pair := strings.SplitN(e, "=", 2)
		// Print the name of the environment variable
		fmt.Println(pair[0])
	}
}

Explanation:

  1. Setting Environment Variable:

    • The program uses os.Setenv to set the value of the "FOO" environment variable to "1".

  2. Retrieving Environment Variable Value:

    • It uses os.Getenv to retrieve and print the value of the "FOO" environment variable.

  3. Checking for Unset Environment Variable:

    • It retrieves and prints the value of the "BAR" environment variable, which is not set. This will print an empty string.

  4. Printing All Environment Variables:

    • The program uses os.Environ to retrieve all environment variables and then iterates through them.

    • For each environment variable, it splits the key-value pair and prints the name of the environment variable.

You can run the program to see the values of the "FOO" and "BAR" environment variables, as well as all other environment variables set in your system:

go run main.go
PreviousCommand-Line SubcommandsNextLogging

Last updated 1 year ago

Was this helpful?