Go Environment Variables: Complete Guide
In Go language, environment variables can be set using the os package. Here is an example code demonstrating how to set an environment variable:
package main
import (
"os"
"fmt"
)
func main() {
// 设置环境变量
os.Setenv("MY_ENV_VAR", "hello")
// 获取环境变量的值
myEnvVar := os.Getenv("MY_ENV_VAR")
fmt.Println("MY_ENV_VAR:", myEnvVar)
}
In the code above, we are using the os.Setenv function to set an environment variable called MY_ENV_VAR.