詳解Go語言中for循環(huán),break和continue的使用
基本語法
和C語言同源的語法格式,有始有終的循環(huán),for init; condition; post { }
帶條件的while循環(huán),for condition { }
無限循環(huán),for { }
有始有終的條件循環(huán)
sum := 0 for i := 0; i < 10; i++ { sum = sum + i }
注意:i變量在循環(huán)結(jié)束后無法使用
帶條件的循環(huán)
count := 0 for count < 10 { fmt.Printf("Current count = %v\n", count) count++ }
無限循環(huán)
由于循環(huán)不會停止,這里使用break來中斷循環(huán),后面還會詳細介紹
count := 0 for { fmt.Printf("current count = %v\n", count) count++ if count > 10 { break } }
數(shù)組循環(huán)
使用計數(shù)器循環(huán)
類似C語言中的循環(huán),我們可以通過計數(shù)器結(jié)合數(shù)組長度實現(xiàn)對數(shù)組的遍歷,同時能獲取數(shù)組索引,如下面例子所示
package main import "fmt" func main() { myarray := []string{"a", "b", "c", "d"} for i := 0; i < len(myarray); i++ { fmt.Printf("Array index is %v, value is %v\n", i, myarray[i]) } }
利用range循環(huán)
利用range可以更容易的進行循環(huán),并且range還能用于slice,channel和map的循環(huán)中
package main import "fmt" func main() { myarray := []string{"a", "b", "c", "d"} for index, item := range myarray { fmt.Printf("current index is %v, value is %v\n", index, item) } }
Map循環(huán)
在介紹Map時,我們已經(jīng)嘗試用for循環(huán)對Map進行遍歷,我們再來鞏固一下
package main import "fmt" func main() { mymap := map[int]string{1 : "a", 2 : "b", 3 : "c"} for key, value := range mymap { fmt.Printf("current key is %v, value is %v\n", key, value) } }
如果只想獲取key,則可以使用,省略value
for key := range mymap {
或者使用_,前面介紹過_無法用于變量,像個占位符
for _, value := range mymap {
string的遍歷
下面的示例是對string類型的遍歷,除了普通的字符,對于Unicode字符切分,字符通常是8位的,UTF-8的字符最高可能是32位的
package main import "fmt" func main() { mystr := "abc" for pos, char := range mystr { fmt.Printf("character '%c' starts at byte position %d\n", char, pos) } for pos, char := range "G?!" { fmt.Printf("character '%c' starts at byte position %d\n", char, pos) } }
character 'G' starts at byte position 0
character '?' starts at byte position 1
character '!' starts at byte position 3
Break和Continue
與大部分語言一致
- Break結(jié)束當前循環(huán)
- Continue開始下一次循環(huán)
package main import "fmt" func main() { for i := 0; i < 10; i++ { if i == 3 { fmt.Printf("For continue at here: %d\n", i) continue } if i > 5 { fmt.Printf("For break at here: %d\n", i) break } fmt.Printf("Current for count: %d\n", i) } fmt.Println("For loop end here") }
輸出結(jié)果
Current for count: 0
Current for count: 1
Current for count: 2
For continue at here: 3
Current for count: 4
Current for count: 5
For break at here: 6
For loop end here
不推薦方式
Go中也支持Lable方式,類似Goto,一般不使用
J: for j := 0; j < 5; j++ { for i := 0; i < 10; i++ { if i > 5 { break J } fmt.Println(i) } }
到此這篇關(guān)于詳解Go語言中for循環(huán),break和continue的使用的文章就介紹到這了,更多相關(guān)Go語言 for break continue內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言中實現(xiàn)打印堆棧的errors包的用法詳解
因為Go語言提供的錯誤太簡單了,以至于簡單的我們無法更好的處理問題,所以誕生了很多對錯誤處理的庫,github.com/pkg/errors是比較簡潔的一樣,本文就來聊聊它的具體用法吧2023-07-07golang如何通過viper讀取config.yaml文件
這篇文章主要介紹了golang通過viper讀取config.yaml文件,圍繞golang讀取config.yaml文件的相關(guān)資料展開詳細內(nèi)容,需要的小伙伴可以參考一下2022-03-03Go結(jié)構(gòu)體SliceHeader及StringHeader作用詳解
這篇文章主要為大家介紹了Go結(jié)構(gòu)體SliceHeader及StringHeader作用的功能及面試官愛問的實際意義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07