@Pac23, there are a few ways you could do this, but the best way depends on how you use these values. The simplest may be to define two maps and initialize them the same way you’re initializing them in Python:
var byName = map[string]int{}
var byVal = map[int]string{}
func init() {
byName["IDS"] = 1413685296
byVal[1413685296] = "IDS"
byName["IRMAP_CACHE"] = 1459634265
byVal[1459634265] = "IRMAP_CACHE"
byName["FS"] = 1363163410
byVal[1363163410] = "FS"
// ...
}
But I’d have other suggestions depending on how these values are used. If you frequently need to lookup these values by their names and the names are referenced elsewhere in the code, it makes sense to turn them into identifiers in the code.
That is, if you are currently doing stuff like this:
resource = acquire_resource(by_name["FS"])
In Go, it’d be better to turn these names into identifiers so you can catch errors like typos at compile-time, before your code is even tested.
If this is true, I’d recommend more abstraction like this:
package main
import (
"fmt"
)
// nameValues is the global collection of NameValuePairs.
var (
nameValues = NewNameValues(1024)
IDS = nameValues.mustDefine(NameValuePair{Name: "IDS", Value: 1413685296})
IRMAP_CACHE = nameValues.mustDefine(NameValuePair{Name: "IRMAP_CACHE", Value: 1459634265})
FS = nameValues.mustDefine(NameValuePair{Name: "FS", Value: 1363163410})
// ...
)
// NameValuePair pairs together a Name and an integer value.
type NameValuePair struct {
Name string
Value int
}
// NameValues is a collection of NameValuePairs indexed by both
// name and value.
type NameValues struct {
// pairs is the actual collection of all of the name value pairs
pairs []NameValuePair
// names is a map of names to the index of the NameValuePair in
// the pairs slice.
names map[string]int
// values is a map of values to the index of the NameValuePair in
// the pairs slice.
values map[int]int
}
// NewNameValues creates a new NameValues collection.
func NewNameValues(capacity int) *NameValues {
return &NameValues{
pairs: make([]NameValuePair, 0, capacity),
names: make(map[string]int, capacity),
values: make(map[int]int, capacity),
}
}
func (nvs *NameValues) mustDefine(p NameValuePair) NameValuePair {
added := nvs.Add(p)
if !added {
panic("redefinition of pair name or value")
}
return p
}
// Add a NameValuePair to the collection if it doesn't conflict with an
// existing entry's name or value.
func (nvs *NameValues) Add(p NameValuePair) (added bool) {
i, ok := nvs.values[p.Value]
if ok {
return false
}
i, ok = nvs.names[p.Name]
if ok {
return false
}
i = len(nvs.pairs)
nvs.pairs = append(nvs.pairs, p)
nvs.names[p.Name] = i
nvs.values[p.Value] = i
return true
}
// ByName gets a NameValuePair by its name if it exists in the
// collection.
func (nvs *NameValues) ByName(name string) (NameValuePair, bool) {
i, ok := nvs.names[name]
if !ok {
return NameValuePair{}, false
}
return nvs.pairs[i], true
}
// ByValue gets a NameValuePair by its name if it exists in the
// collection.
func (nvs *NameValues) ByValue(value int) (NameValuePair, bool) {
i, ok := nvs.values[value]
if !ok {
return NameValuePair{}, false
}
return nvs.pairs[i], true
}
func main() {
fmt.Println("IDS:", IDS.Value)
fmt.Println("1459634265:", IRMAP_CACHE.Name)
}
This still assumes that the identifiers don’t change throughout the runtime of the program. If the identifiers are dynamic and you need to access them by strings, it is probably good enough to use the two-map approach instead.