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")
)
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.