<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國際加速解決方案。 廣告
                [TOC] 如果想要重復執行某些語句,Go 語言中您只有 for 結構可以使用。不要小看它,這個 for 結構比其它語言中的更為靈活。 **注意事項**?其它許多語言中也沒有發現和 do while 完全對等的 for 結構,可能是因為這種需求并不是那么強烈。 ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.4.md#541-基于計數器的迭代)5.4.1 基于計數器的迭代 文件 for1.go 中演示了最簡單的基于計數器的迭代,基本形式為: ~~~ for 初始化語句; 條件語句; 修飾語句 {} ~~~ 示例 5.6?[for1.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_5/for1.go): ~~~ package main import "fmt" func main() { for i := 0; i < 5; i++ { fmt.Printf("This is the %d iteration\n", i) } } ~~~ 輸出: ~~~ This is the 0 iteration This is the 1 iteration This is the 2 iteration This is the 3 iteration This is the 4 iteration ~~~ 由花括號括起來的代碼塊會被重復執行已知次數,該次數是根據計數器(此例為 i)決定的。循環開始前,會執行且僅會執行一次初始化語句?`i := 0;`;這比在循環之前聲明更為簡短。緊接著的是條件語句?`i < 5;`,在每次循環開始前都會進行判斷,一旦判斷結果為 false,則退出循環體。最后一部分為修飾語句?`i++`,一般用于增加或減少計數器。 這三部分組成的循環的頭部,它們之間使用分號?`;`?相隔,但并不需要括號?`()`?將它們括起來。例如:`for (i = 0; i < 10; i++) { }`,這是無效的代碼! 同樣的,左花括號?`{`?必須和 for 語句在同一行,計數器的生命周期在遇到右花括號?`}`?時便終止。一般習慣使用 i、j、z 或 ix 等較短的名稱命名計數器。 特別注意,永遠不要在循環體內修改計數器,這在任何語言中都是非常差的實踐! 您還可以在循環中同時使用多個計數器: ~~~ for i, j := 0, N; i < j; i, j = i+1, j-1 {} ~~~ 這得益于 Go 語言具有的平行賦值的特性(可以查看第 7 章 string_reverse.go 中反轉數組的示例)。 您可以將兩個 for 循環嵌套起來: ~~~ for i:=0; i<5; i++ { for j:=0; j<10; j++ { println(j) } } ~~~ 如果您使用 for 循環迭代一個 Unicode 編碼的字符串,會發生什么? 示例 5.7?[for_string.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_5/for_string.go): ~~~ package main import "fmt" func main() { str := "Go is a beautiful language!" fmt.Printf("The length of str is: %d\n", len(str)) for ix :=0; ix < len(str); ix++ { fmt.Printf("Character on position %d is: %c \n", ix, str[ix]) } str2 := "日本語" fmt.Printf("The length of str2 is: %d\n", len(str2)) for ix :=0; ix < len(str2); ix++ { fmt.Printf("Character on position %d is: %c \n", ix, str2[ix]) } } ~~~ 輸出: ~~~ The length of str is: 27 Character on position 0 is: G Character on position 1 is: o Character on position 2 is: Character on position 3 is: i Character on position 4 is: s Character on position 5 is: Character on position 6 is: a Character on position 7 is: Character on position 8 is: b Character on position 9 is: e Character on position 10 is: a Character on position 11 is: u Character on position 12 is: t Character on position 13 is: i Character on position 14 is: f Character on position 15 is: u Character on position 16 is: l Character on position 17 is: Character on position 18 is: l Character on position 19 is: a Character on position 20 is: n Character on position 21 is: g Character on position 22 is: u Character on position 23 is: a Character on position 24 is: g Character on position 25 is: e Character on position 26 is: ! The length of str2 is: 9 Character on position 0 is: ? Character on position 1 is: ? Character on position 2 is: ¥ Character on position 3 is: ? Character on position 4 is: ? Character on position 5 is: ? Character on position 6 is: è Character on position 7 is: a Character on position 8 is: ? ~~~ 如果我們打印 str 和 str2 的長度,會分別得到 27 和 9。 由此我們可以發現,ASCII 編碼的字符占用 1 個字節,既每個索引都指向不同的字符,而非 ASCII 編碼的字符(占有 2 到 4 個字節)不能單純地使用索引來判斷是否為同一個字符。我們會在第 5.4.4 節解決這個問題。 ### [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.4.md#練習題)練習題 **練習 5.4**?[for_loop.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_5/for_loop.go) 1. 使用 for 結構創建一個簡單的循環。要求循環 15 次然后使用 fmt 包來打印計數器的值。 2. 使用 goto 語句重寫循環,要求不能使用 for 關鍵字。 **練習 5.5**?[for_character.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_5/for_character.go) 創建一個程序,要求能夠打印類似下面的結果(直到每行 25 個字符時為止): ~~~ G GG GGG GGGG GGGGG GGGGGG ~~~ 1. 使用 2 層嵌套 for 循環。 2. 使用一層 for 循環以及字符串截斷。 **練習 5.6**?[bitwise_complement.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_5/bitwise_complement.go) 使用按位補碼從 0 到 10,使用位表達式?`%b`?來格式化輸出。 **練習 5.7**?Fizz-Buzz 問題:[fizzbuzz.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_5/fizzbuzz.go) 寫一個從 1 打印到 100 的程序,但是每當遇到 3 的倍數時,不打印相應的數字,但打印一次 "Fizz"。遇到 5 的倍數時,打印?`Buzz`?而不是相應的數字。對于同時為 3 和 5 的倍數的數,打印?`FizzBuzz`(提示:使用 switch 語句)。 **練習 5.8**?Fizz-Buzz 問題:[rectangle_stars.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_5/rectangle_stars.go) 使用?`*`?符號打印寬為 20,高為 10 的矩形。 ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.4.md#542-基于條件判斷的迭代)5.4.2 基于條件判斷的迭代 for 結構的第二種形式是沒有頭部的條件判斷迭代(類似其它語言中的 while 循環),基本形式為:`for 條件語句 {}`。 您也可以認為這是沒有初始化語句和修飾語句的 for 結構,因此?`;;`?便是多余的了。 Listing 5.8?[for2.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_5/for2.go): ~~~ package main import "fmt" func main() { var i int = 5 for i >= 0 { i = i - 1 fmt.Printf("The variable i is now: %d\n", i) } } ~~~ 輸出: ~~~ The variable i is now: 4 The variable i is now: 3 The variable i is now: 2 The variable i is now: 1 The variable i is now: 0 The variable i is now: -1 ~~~ ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.4.md#543-無限循環)5.4.3 無限循環 條件語句是可以被省略的,如?`i:=0; ; i++`?或?`for { }`?或?`for ;; { }`(`;;`?會在使用 gofmt 時被移除):這些循環的本質就是無限循環。最后一個形式也可以被改寫為?`for true { }`,但一般情況下都會直接寫?`for { }`。 如果 for 循環的頭部沒有條件語句,那么就會認為條件永遠為 true,因此循環體內必須有相關的條件判斷以確保會在某個時刻退出循環。 想要直接退出循環體,可以使用 break 語句(第 5.5 節)或 return 語句直接返回(第 6.1 節)。 但這兩者之間有所區別,break 只是退出當前的循環體,而 return 語句提前對函數進行返回,不會執行后續的代碼。 無限循環的經典應用是服務器,用于不斷等待和接受新的請求。 ~~~ for t, err = p.Token(); err == nil; t, err = p.Token() { ... } ~~~ ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/05.4.md#544-for-range-結構)5.4.4 for-range 結構 這是 Go 特有的一種的迭代結構,您會發現它在許多情況下都非常有用。它可以迭代任何一個集合(包括數組和 map,詳見第 7 和 8 章)。語法上很類似其它語言中 foreach 語句,但您依舊可以獲得每次迭代所對應的索引。一般形式為:`for ix, val := range coll { }`。 要注意的是,`val`?始終為集合中對應索引的值拷貝,因此它一般只具有只讀性質,對它所做的任何修改都不會影響到集合中原有的值(**譯者注:如果?`val`?為指針,則會產生指針的拷貝,依舊可以修改集合中的原值**)。一個字符串是 Unicode 編碼的字符(或稱之為?`rune`)集合,因此您也可以用它迭代字符串: ~~~ for pos, char := range str { ... } ~~~ 每個 rune 字符和索引在 for-range 循環中是一一對應的。它能夠自動根據 UTF-8 規則識別 Unicode 編碼的字符。 示例 5.9?[range_string.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_5/range_string.go): ~~~ package main import "fmt" func main() { str := "Go is a beautiful language!" fmt.Printf("The length of str is: %d\n", len(str)) for pos, char := range str { fmt.Printf("Character on position %d is: %c \n", pos, char) } fmt.Println() str2 := "Chinese: 日本語" fmt.Printf("The length of str2 is: %d\n", len(str2)) for pos, char := range str2 { fmt.Printf("character %c starts at byte position %d\n", char, pos) } fmt.Println() fmt.Println("index int(rune) rune char bytes") for index, rune := range str2 { fmt.Printf("%-2d %d %U '%c' % X\n", index, rune, rune, rune, []byte(string(rune))) } } ~~~ 輸出: ~~~ The length of str is: 27 Character on position 0 is: G Character on position 1 is: o Character on position 2 is: Character on position 3 is: i Character on position 4 is: s Character on position 5 is: Character on position 6 is: a Character on position 7 is: Character on position 8 is: b Character on position 9 is: e Character on position 10 is: a Character on position 11 is: u Character on position 12 is: t Character on position 13 is: i Character on position 14 is: f Character on position 15 is: u Character on position 16 is: l Character on position 17 is: Character on position 18 is: l Character on position 19 is: a Character on position 20 is: n Character on position 21 is: g Character on position 22 is: u Character on position 23 is: a Character on position 24 is: g Character on position 25 is: e Character on position 26 is: ! The length of str2 is: 18 character C starts at byte position 0 character h starts at byte position 1 character i starts at byte position 2 character n starts at byte position 3 character e starts at byte position 4 character s starts at byte position 5 character e starts at byte position 6 character : starts at byte position 7 character starts at byte position 8 character 日 starts at byte position 9 character 本 starts at byte position 12 character 語 starts at byte position 15 index int(rune) rune char bytes 0 67 U+0043 'C' 43 1 104 U+0068 'h' 68 2 105 U+0069 'i' 69 3 110 U+006E 'n' 6E 4 101 U+0065 'e' 65 5 115 U+0073 's' 73 6 101 U+0065 'e' 65 7 58 U+003A ':' 3A 8 32 U+0020 ' ' 20 9 26085 U+65E5 '日' E6 97 A5 12 26412 U+672C '本' E6 9C AC 15 35486 U+8A9E '語' E8 AA 9E ~~~ 請將輸出結果和 Listing 5.7(for_string.go)進行對比。 我們可以看到,常用英文字符使用 1 個字節表示,而中文字符使用 3 個字符表示。 **練習 5.9**?以下程序的輸出結果是什么? ~~~ for i := 0; i < 5; i++ { var v int fmt.Printf("%d ", v) v = 5 } ~~~ **問題 5.2:**?請描述以下 for 循環的輸出結果: 1. ~~~ for i := 0; ; i++ { fmt.Println("Value of i is now:", i) } ~~~ 2. ~~~ for i := 0; i < 3; { fmt.Println("Value of i:", i) } ~~~ 3. ~~~ s := "" for ; s != "aaaaa"; { fmt.Println("Value of s:", s) s = s + "a" } ~~~ 4. ~~~ for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1, j+1, s + "a" { fmt.Println("Value of i, j, s:", i, j, s) } ~~~
                  <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>

                              哎呀哎呀视频在线观看