Go doesn't have array constants.
My application receives messages containing several types of numeric codes which I need to display as phrases.
If array contants existed I could do something like:
func foo() {
...
fmt.Println(facename[f])
...
}
const facename [...]string = "top", "bottom", "left", "right", "front", "back"
But of course there's no way to do this. The first way around this that occurs to me, and maybe a reasonable efficient one is to use a switch
func foo() {
...
name := "unknown"
switch f {
case 0:
name = "top"
case 1:
name = "bottom"
case 2:
name = "left"
case 3:
name = "right"
case 4:
name = "front"
case 5:
name = "back"
}
fmt.Println(name)
...
}
The above is rather tedious if the number of values gets to be twenty or more.
It seems the most concise way is something like
func foo() {
...
fmt.Println(strings.Split(facenames,",")[f])
...
}
const facenames = "top,bottom,left,right,front,back"
I will also have to check that the index is in range of course.
Although efficiency isn't a concern at the moment, it bugs me that
I'm using strings.Split()
more than I want to.
Is there another way that is either idiomatic or both concise and efficient?