Constants in Go set during init

My Go program has configuration values ​​that I want to be constant throughout the execution of the program, but I want to be able to change at the deployment location. As far as I can tell, there is no way to achieve this with the const keyword, since (again, as far as I can tell) its value should be the constant specified at compile time. This means that the only way to achieve what I want is to declare normal variables and initialize them during the batch init function. It is not that this will not work, but rather that there will now be nothing to prevent these pseudo-constant values ​​from changing.

My two questions are:

  • Am I missing something about how const works?
  • Assuming this is not the case, what is the preferred way to handle this? A public function that returns a private variable that I never expand without ever changing it? Just hoping people don’t change the variables as they really are configuration settings?
+4
source share
5 answers

Create the "config.go" file and create the vars that you want to open.

Do not export them (do everything in lower case). Instead, create a public (uppercase) func that provides access to each element.

 package config var x = 10 func X() int { return x } 

If you need these variables, you simply import ./config and use them in your code as follows:

 if config.X() 

Obviously, you can set the variables in the init package.

+2
source

The following code is almost the same as the second @Christopher method, except that it is not a module, it is in the main package.

 package main import ( "os" ) type Config struct { debug bool key string proxyNumber int } func (c *Config) Debug() bool { return c.debug } func (c *Config) Key() string { return c.key } func (c *Config) ProxyNumber() int { return c.proxyNumber } const ( CONFIG_NAME = "config.ini" ) var config *Config func init() { DefaultConfig() if Exists(CONFIG_NAME) { //try to save the config file }else { //try to load from the config file } } func DefaultConfig() { config = &Config{debug:true, key:"abcde", proxyNumber:5, } } //Exist: check the file exist func Exists(path string) bool { _, err := os.Stat(path) if err == nil { return true } if os.IsNotExist(err) { return false } return false } 

you can use config to load and save the configuration file.

+1
source

This is a very good question because it delves into what, I suspect, may be an omission from Go, an unchanging state.

From a link to the language , "constant expressions can only contain constant operands and are evaluated at compile time."

You cannot make wars permanent, which is a shame. Answer Joe offers encapsulation as a solution that will work well, but it is verbose, tedious, and can lead to errors.

In comparison, many impure functional languages ​​combine mutable variables with immutable values ​​with a single assignment. For example, Scala has the keywords "val" and "var"; the value of Scala 'var' is very similar to Go 'var'. Invariance is a useful tool in the toolbar because link-transparent functions that are free from side effects can be written along with state-change code. Both have their place. Consistency is also a valuable tool for concurrency, because there is no problem regarding the possible conditions of a race if fixed values ​​are shared between goroutines.

Thus, in my opinion, among many of his strengths, this is one of Go's drawbacks. Apparently, it would not be easy to maintain shafts, as well as vars, the difference being that the compiler checks that each val is assigned exactly once.

Until this feature is added, you have encapsulation as the only option.

+1
source

You can do something like this:

 package main import ( "fmt" "strconv" ) var a string func main() { myvar, err := strconv.Atoi(a) if err != nil { fmt.Println(err) } fmt.Println(myvar) } 

and compile the program with

 go build -ldflags '-X main.a 10' test.go 

This way you can define a constant at compile time.

0
source

Just use the standard go flags with iniflags , the standard launch flags allow you to set arbitrary configuration variables when the program starts, passing command line flags, and iniflags "magically" add support for reading configuration variables from ini files.

0
source

All Articles