Declare constants in golang using the results of os.Getenv in the const initializer in os.Getenv ("MY_SECRET") is not a constant '

If I declare constants as follows, I get a const error initializer in os.Getenv ("MY_SECRET") is not a constant. "Why is this?

New to Go, and I see that the Getenv return type is a string, but I don’t understand why this will not work as a constant.

const ( secret = os.Getenv("MY_SECRET") key = os.Getenv("MY_KEY") ) 
+5
source share
1 answer

As in the case of an error, the constant must have a constant value . You cannot set it to return a function. It should be evaluated at compile time (e.g. string literal). If you want to store the values ​​of the environment variables that you searched at run time, you will have to store them in variables, not in constants.

+7
source

Source: https://habr.com/ru/post/1272023/


All Articles