<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                switch 語句 switch 語句用于基于不同條件執行不同動作,每一個 case 分支都是唯一的,從上直下逐一測試,直到匹配為止。 Golang switch 分支表達式可以是任意類型,不限于常量。可省略 break,默認自動終止。 語法 Go 編程語言中 switch 語句的語法如下: ~~~ switch var1 { case val1: ... case val2: ... default: ... } ~~~ 變量 var1 可以是任何類型,而 val1 和 val2 則可以是同類型的任意值。類型不被局限于常量或整數,但必須是相同的類型;或者最終結果為相同類型的表達式。 您可以同時測試多個可能符合條件的值,使用逗號分割它們,例如:case val1, val2, val3。 實例: ~~~ package main import "fmt" func main() { /* 定義局部變量 */ var grade string = "B" var marks int = 90 switch marks { case 90: grade = "A" case 80: grade = "B" case 50,60,70 : grade = "C" default: grade = "D" } switch { case grade == "A" : fmt.Printf("優秀!\n" ) case grade == "B", grade == "C" : fmt.Printf("良好\n" ) case grade == "D" : fmt.Printf("及格\n" ) case grade == "F": fmt.Printf("不及格\n" ) default: fmt.Printf("差\n" ) } fmt.Printf("你的等級是 %s\n", grade ) } ~~~ 以上代碼執行結果為: ~~~ 優秀! 你的等級是 A ~~~ Type Switch switch 語句還可以被用于 type-switch 來判斷某個 interface 變量中實際存儲的變量類型。 ~~~ Type Switch 語法格式如下: switch x.(type){ case type: statement(s) case type: statement(s) /* 你可以定義任意個數的case */ default: /* 可選 */ statement(s) } ~~~ 實例: ~~~ package main import "fmt" func main() { var x interface{} //寫法一: switch i := x.(type) { // 帶初始化語句 case nil: fmt.Printf(" x 的類型 :%T\r\n", i) case int: fmt.Printf("x 是 int 型") case float64: fmt.Printf("x 是 float64 型") case func(int) float64: fmt.Printf("x 是 func(int) 型") case bool, string: fmt.Printf("x 是 bool 或 string 型") default: fmt.Printf("未知型") } //寫法二 var j = 0 switch j { case 0: case 1: fmt.Println("1") case 2: fmt.Println("2") default: fmt.Println("def") } //寫法三 var k = 0 switch k { case 0: println("fallthrough") fallthrough /* Go的switch非常靈活,表達式不必是常量或整數,執行的過程從上至下,直到找到匹配項; 而如果switch沒有表達式,它會匹配true。 Go里面switch默認相當于每個case最后帶有break, 匹配成功后不會自動向下執行其他case,而是跳出整個switch, 但是可以使用fallthrough強制執行后面的case代碼。 */ case 1: fmt.Println("1") case 2: fmt.Println("2") default: fmt.Println("def") } //寫法三 var m = 0 switch m { case 0, 1: fmt.Println("1") case 2: fmt.Println("2") default: fmt.Println("def") } //寫法四 var n = 0 switch { //省略條件表達式,可當 if...else if...else case n > 0 && n < 10: fmt.Println("i > 0 and i < 10") case n > 10 && n < 20: fmt.Println("i > 10 and i < 20") default: fmt.Println("def") } } ~~~ 以上代碼執行結果為: ~~~ x 的類型 :<nil> fallthrough 1 1 def ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看