How to declare a constant map in Go?
Hi everyone! Just started learning Go and ran into a problem. I need to declare a map whose values should not change during program execution. Essentially, it should be a constant.
In other languages, like C++ or Java, you can use `static final` or `const`. In Go, I know there is the `const` keyword, but when I try to declare a map with it, the compiler gives an error.
Here's what I tried:
```go
const myMap = map[string]int{
"apple": 5,
"banana": 3,
}
```
The compiler says: `const initializer map[string]int{...} is not a constant`. I understand that Go is strict about constants, but how do I declare an immutable map? I need this map to be accessible from different parts of the program, but no one should be able to modify it (add/remove elements or
No answers yet.
Log in to leave an answer