If I declare constants as the following I get the error 'const initializer in os.Getenv("MY_SECRET") is not a constant'. Why is this?
New to Go and I see the return type of Getenv is a string, but I don't understand why this wouldn't work as a constant.
const ( secret = os.Getenv("MY_SECRET") key = os.Getenv("MY_KEY") )
Go os. The Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present. To distinguish between an empty value and an unset value, use LookupEnv . get_env.go.
Go does not support constants in structs. which is an invalid syntax and also has no semantic meaning. If you want to assign each game variable a game ID, you can add an unexported field to the struct, as well as a method that returns the ID value.
Just like the error says, a constant must have a constant value. You cannot set it to the return of a function. It must be evaluated at compile time (e.g. a string literal). If you want to store the values of environment variables looked up at run time, you'll have to store them in variables, not constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With