To get the value of an environment variable using the os
package in Go, you can make use of the Getenv
function. Here's an example:
package main
import (
"fmt"
"os"
)
func main() {
// Get the value of a specific environment variable
value := os.Getenv("MY_VARIABLE")
// Check if the environment variable is set
if value == "" {
fmt.Println("Environment variable not set")
} else {
fmt.Println("Value:", value)
}
}
In this example, we use os.Getenv
function to retrieve the value of an environment variable with the name "MY_VARIABLE". The function returns an empty string if the environment variable is not set.