英文:
Workaround for lack of support for array constants?
问题
Go语言没有数组常量。
我的应用程序接收包含多种类型的数字代码的消息,我需要将其显示为短语。
如果存在数组常量,我可以这样做:
func foo() {
...
fmt.Println(facename[f])
...
}
const facename [...]string = "top", "bottom", "left", "right", "front", "back"
但是当然没有办法这样做。我能想到的第一种解决方法,也许是一种合理高效的方法,是使用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)
...
}
如果值的数量达到二十个或更多,上述方法会变得相当繁琐。
似乎最简洁的方法是这样的:
func foo() {
...
fmt.Println(strings.Split(facenames, ",")[f])
...
}
const facenames = "top,bottom,left,right,front,back"
当然,我还需要检查索引是否在范围内。虽然效率目前不是问题,但我不希望过多地使用strings.Split()
。
是否有其他既符合惯用方式又简洁高效的方法?
英文:
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:
<!-- language: go -->
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
<!-- language: go -->
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
<!-- language: go -->
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?
答案1
得分: 2
惯用的方法是使用包级别的变量:
var facename = []string{"top", "bottom", "left", "right", "front", "back"}
func foo() {
// ...
fmt.Println(facename[f])
// ...
}
在这种情况下,使用切片而不是数组也是惯用的做法。
英文:
The idiomatic approach is to use a package level variable:
var facename = []string{"top", "bottom", "left", "right", "front", "back"}
func foo() {
// ...
fmt.Println(facename[f])
// ...
}
It's also idiomatic to use a slice instead of an array in this situation.
答案2
得分: 0
可能并不是一个很好的匹配,但是你的用例似乎基本上是一个具有字符串等效项的枚举。对于这种情况,我通常会采取以下方式:
type Face uint8
const (
FaceTop Face = iota
FaceBottom
FaceLeft
FaceRight
FaceFront
FaceBack
)
func (f Face) String() string {
switch f {
case FaceTop: return "top"
case FaceBottom: return "bottom"
case FaceLeft: return "left"
case FaceRight: return "right"
case FaceFront: return "front"
case FaceBack: return "back"
default: return "" // 或者添加一个错误返回,或者在枚举中添加一个零值
}
}
func ParseFace(in string) (Face, error) {
switch in {
case "top": return FaceTop, nil
case "bottom": return FaceBottom, nil
case "left": return FaceLeft, nil
case "right": return FaceRight, nil
case "front": return FaceFront, nil
case "back": return FaceBack, nil
default: return "", errors.New("无效的面名称" + in)
}
}
以上是一个示例代码,用于将枚举类型与字符串等效项进行转换。
英文:
It may not be a good fit, but it appears your use case is basically an enum with string equivalents. For this I usually do something along the lines of:
type Face uint8
const (
FaceTop Face = iota
FaceBottom
FaceLeft
FaceRight
FaceFront
FaceBack
)
func (f Face) String() string {
switch f {
case FaceTop: return "top"
case FaceBottom: return "bottom"
case FaceLeft: return "left"
case FaceRight: return "right"
case FaceFront: return "front"
case FaceBack: return "back"
default: return "" // Or add an error return, or add a zero value to the enum
}
}
func ParseFace(in string) (Face,error) {
switch in {
case "top": return FaceTop,nil
case "bottom": return FaceBottom,nil
case "left": return FaceLeft,nil
case "right": return FaceRight,nil
case "front": return FaceFront,nil
case "back": return FaceBack,nil
default: return "",errors.New("invalid face name"+in)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论