Go by Example: Environment Variables

package main

import (

func main() {
	os.Setenv("FOO", "1")
	fmt.Println("FOO:", os.Getenv("FOO"))
	fmt.Println("BAR:", os.Getenv("BAR"))

	for _, e := range os.Environ() {

In this example, the os package’s Setenv and Getenv functions are used to set and get an environment variable named FOO. The os.Environ function returns a slice of strings representing all environment variables, which are then printed out.


Leave a Reply

Your email address will not be published. Required fields are marked *