I
I
Ivan Volkov2015-10-08 16:02:06
go
Ivan Volkov, 2015-10-08 16:02:06

How in GO to beat the moment with the creation of a global constant []string / map[string]string?

Good day.
I just took up GO, so I would like to know how to properly beat this moment in the GO way style t.s.
The Golang spec says that constants can only be - "numbers, strings or Booleans".
Then tell me how to do this option:

package main
...
const (
...
  SOME_PARAMS = []string {"param1", "param2"}
  // либо
  SOME_PARAMS  = map[string]string {
    "key1": "val1",
    "key2": "val2",
  }
)
...
func main() {
}

Create some init () function and assign it in it? But then it will no longer be a constant...
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2015-10-08
@oOLokiOo

Slice and map are dynamic by definition, i.e. operate on the heap unless the compiler optimizes them. They cannot be constants, the creators have about the same ideology.
And you can create without just using PS instead of slice init()in your code To use a regular array instead of slice (our speed is everything), then writeconstvar
[...]string {"param1", "param2"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question