使用切片值作为case,在switch语句中匹配一个值。

huangapple go评论119阅读模式
英文:

Go match a value by using slices of values as cases with switch statement

问题

我知道你可以使用逗号将多个值分隔开来,在switch语句中匹配多个值:

  1. func main() {
  2. value := 5
  3. switch value {
  4. case 1, 2, 3:
  5. fmt.Println("匹配1、2或3")
  6. case 4, 5, 6:
  7. fmt.Println("匹配4、5或6")
  8. }
  9. }

我的问题是,你能否使用包含多个值的切片作为case来匹配多个值?我知道可以通过使用if else语句和Contains(slice, element)函数来实现,我只是想知道是否可能。

像这样的代码可能行吗?

  1. func main() {
  2. value := 5
  3. low := []int{1, 2, 3}
  4. high := []int{4, 5, 6}
  5. switch value {
  6. case low:
  7. fmt.Println("匹配1、2或3")
  8. case high:
  9. fmt.Println("匹配4、5或6")
  10. }
  11. }
英文:

I know you can match multiple values with the switch statement by separating values with commas:

  1. func main() {
  2. value := 5
  3. switch value{
  4. case 1,2,3:
  5. fmt.Println("matches 1,2 or 3")
  6. case 4,5, 6:
  7. fmt.Println("matches 4,5 or 6")
  8. }
  9. }

http://play.golang.org/p/D_2Zp8bW5M

My question is, can you match multiple values with the switch statement by using slice(s) of multiple values as case(s)? I know It can be done by using if else statements and a 'Contains(slice, element)' function, I am just wondering if its possible.

Something like this maybe?:

  1. func main() {
  2. value := 5
  3. low := []int{1, 2, 3}
  4. high := []int{4, 5, 6}
  5. switch value {
  6. case low:
  7. fmt.Println("matches 1,2 or 3")
  8. case high:
  9. fmt.Println("matches 4,5 or 6")
  10. }
  11. }

答案1

得分: 13

你可以得到的最好的可能是这个:

  1. package main
  2. import "fmt"
  3. func contains(v int, a []int) bool {
  4. for _, i := range a {
  5. if i == v {
  6. return true
  7. }
  8. }
  9. return false
  10. }
  11. func main() {
  12. first := []int{1, 2, 3}
  13. second := []int{4, 5, 6}
  14. value := 5
  15. switch {
  16. case contains(value, first):
  17. fmt.Println("匹配第一个")
  18. case contains(value, second):
  19. fmt.Println("匹配第二个")
  20. }
  21. }

这是一个Go语言的代码示例,它定义了一个名为contains的函数,用于检查一个整数是否存在于一个整数切片中。在main函数中,它创建了两个整数切片firstsecond,然后使用switch语句检查变量value是否存在于这两个切片中,并打印相应的结果。

英文:

The best you can get is probably this one:

  1. package main
  2. import "fmt"
  3. func contains(v int, a []int) bool {
  4. for _, i := range a {
  5. if i == v {
  6. return true
  7. }
  8. }
  9. return false
  10. }
  11. func main() {
  12. first := []int{1, 2, 3}
  13. second := []int{4, 5, 6}
  14. value := 5
  15. switch {
  16. case contains(value, first):
  17. fmt.Println("matches first")
  18. case contains(value, second):
  19. fmt.Println("matches second")
  20. }
  21. }

答案2

得分: 2

如果你对切片有控制权,你可以使用映射(maps)代替:

  1. package main
  2. func main() {
  3. var (
  4. value = 5
  5. low = map[int]bool{1: true, 2: true, 3: true}
  6. high = map[int]bool{4: true, 5: true, 6: true}
  7. )
  8. switch {
  9. case low[value]:
  10. println("匹配 1、2 或 3")
  11. case high[value]:
  12. println("匹配 4、5 或 6")
  13. }
  14. }

或者如果所有的数字都在 256 以下,你可以使用字节切片(bytes):

  1. package main
  2. import "bytes"
  3. func main() {
  4. var (
  5. value = []byte{5}
  6. low = []byte{1, 2, 3}
  7. high = []byte{4, 5, 6}
  8. )
  9. switch {
  10. case bytes.Contains(low, value):
  11. println("匹配 1、2 或 3")
  12. case bytes.Contains(high, value):
  13. println("匹配 4、5 或 6")
  14. }
  15. }
英文:

If you have control over the slices, you could just use maps instead:

  1. package main
  2. func main() {
  3. var (
  4. value = 5
  5. low = map[int]bool{1: true, 2: true, 3: true}
  6. high = map[int]bool{4: true, 5: true, 6: true}
  7. )
  8. switch {
  9. case low[value]:
  10. println("matches 1,2 or 3")
  11. case high[value]:
  12. println("matches 4,5 or 6")
  13. }
  14. }

or if all the numbers are under 256, you can use bytes:

  1. package main
  2. import "bytes"
  3. func main() {
  4. var (
  5. value = []byte{5}
  6. low = []byte{1, 2, 3}
  7. high = []byte{4, 5, 6}
  8. )
  9. switch {
  10. case bytes.Contains(low, value):
  11. println("matches 1,2 or 3")
  12. case bytes.Contains(high, value):
  13. println("matches 4,5 or 6")
  14. }
  15. }

答案3

得分: 0

不,由于语言规范的限制,你不能这样做。
最简单的方法有两种:

  1. 对于动态值,可以使用唯一的集合(map[value]struct{})。
  2. 对于静态值,可以直接在switch case中打印值。
英文:

No, you can't do this because of the language specification.
The easiest ways are:

  1. using unique sets(map[value]struct{}) for dynamic values
  2. printing values straight in switch case for static

huangapple
  • 本文由 发表于 2015年8月6日 17:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/31851842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定