How I can rewrite the below code so that whenever I use the var CsvVersion
every time it calls the func and gives me the latest value?
package main
import (
"fmt"
"os"
)
var (
DefaultValMap = map[string]string{
"CSV_VERSION": "v1",
}
CsvVersion = GetEnvOrDefault("CSV_VERSION")
)
func GetEnvOrDefault(env string) string {
if val := os.Getenv(env); val != "" {
return val
}
return DefaultValMap[env]
}
func main() {
fmt.Println(CsvVersion)
os.Setenv("CSV_VERSION", "v2")
fmt.Println(CsvVersion)
fmt.Println(os.Getenv("CSV_VERSION"))
}
Actual output
$ go build 1.go && ./1
v1
v1
v2
The output should be like this
$ go build 1.go && ./1
v1
v2
v2