Go 学习笔记(62)— Go 中 switch 语句中的 switch 表达式和 case 表达式之间的关系
发布日期:2021-05-10 08:44:37 浏览次数:18 分类:精选文章

本文共 3852 字,大约阅读时间需要 12 分钟。

Go���������switch������������������������������������������������������������������������������������������������������������������������Goswitch���������������case���������������������������������������������

  • switch���������case���������������

    • ���������������
      package main
      import "fmt"
      func main() {
      value1 := [...]int8{0, 1, 2, 3, 4, 5, 6}
      switch 1 + 3 {
      case value1[0], value1[1]:
      fmt.Println("0 or 1")
      case value1[2], value1[3]:
      fmt.Println("2 or 3")
      case value1[4], value1[5], value1[6]:
      fmt.Println("4 or 5 or 6")
      }
      }
      • ���������
        invalid case value1[0] in switch on 1 + 3 (mismatched types int8 and int)
        invalid case value1[1] in switch on 1 + 3 (mismatched types int8 and int)
    • ���������
      • switch���������7���������������4������������������������int���
      • case������������������������int8���������������������������������
    • ���������������
      • ������switch������������case���������������������������������
        package main
        import "fmt"
        func main() {
        value1 := [...]int8{0, 1, 2, 3, 4, 5, 6}
        switch val := 1 + 3 {
        case int8(val):
        // ������������������������
        ...
        }
        }
  • switch���������case���������������������

    • ���������������
      package main
      import "fmt"
      func main() {
      value2 := [...]int8{0, 1, 2, 3, 4, 5, 6}
      switch value2[4] {
      case 0, 1:
      fmt.Println("0 or 1")
      case 2, 3:
      fmt.Println("2 or 3")
      case 4, 5, 6:
      fmt.Println("4 or 5 or 6")
      }
      }
      • ���������������
        • ������case������������������������������������������������������������switch���������
        • ���������������������������������������������������������
  • case������������������������

    • ���������������
      package main
      import "fmt"
      func main() {
      value3 := [...]int8{0, 1, 2, 3, 4, 5, 6}
      switch value3[4] {
      case 0, 1, 2:
      fmt.Println("0 or 1 or 2")
      case 2, 3, 4:
      fmt.Println("2 or 3 or 4")
      case 4, 5, 6:
      fmt.Println("4 or 5 or 6")
      }
      }
      • ���������
        duplicate case 2 in switch
        duplicate case 4 in switch
      • ���������
        • ������case������������������������������������������������������
    • ���������������
      • ������������������������
        switch value3[4] {
        case 0, 1, 2:
        ...
        case 4:
        ... // ���������������case������������
        case 5, 6:
        ...
        }
  • case���������������������������������������

    • ���������������
      package main
      import "fmt"
      func main() {
      value5 := [...]int8{0, 1, 2, 3, 4, 5, 6}
      switch value5[4] {
      case value5[0], value5[1], value5[2]:
      fmt.Println("0 or 1 or 2")
      case value5[2], value5[3], value5[4]:
      fmt.Println("2 or 3 or 4")
      case value5[4], value5[5], value5[6]:
      fmt.Println("4 or 5 or 6")
      }
      }
      • ���������������
        • ������������������������������������������������������������������
        • ������������������������������������
      • ���������������������
        case include sub expressions that can be the same value
        switch val:
        case val == a:
        ...
        case val == b:
        ...
  • ������switch���������������������������������

    • ���������������
      package main
      import "fmt"
      func main() {
      value6 := interface{
      (byte(127))
      }
      switch t := value6.(type) {
      case uint8, uint16:
      fmt.Println("uint8 or uint16")
      case byte:
      fmt.Printf("byte")
      default:
      fmt.Printf("unsupported type: %T", t)
      }
      }
      • ���������
        duplicate case byte in type switch
    • ���������
      • byte���uint8���������������������������������switch������������������
    • ���������������
      • ���������������������
        case uint8:
        ...
        case byte: // ���uint8������������������������
      • ������������case������������������������������������������������������������
  • ������������and������������������������������������������Go������switch���������������������������������������������������������������

    上一篇:Go 学习笔记(63)— Go 中的 for ... range 对切片和数组的差异
    下一篇:Go 学习笔记(65)— Go 中函数参数是传值还是传引用

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年04月23日 19时38分31秒