Go语言循环语句详细指南

2021年3月14日14:32:32 发表评论 583 次浏览

Go语言仅包含一个for循环。 for循环是一种重复控制结构, 它使我们可以编写执行特定次数的循环。在Go语言中, 此for循环可以以不同的形式使用, 形式包括:

1. for循环简单与我们在其他编程语言(例如C, C ++, Java, C#等)中使用的相似。

语法如下:

for initialization; condition; post{
       // statements....
}

这里,

  • 的初始化语句是可选的, 在for循环开始之前执行。初始化语句始终位于简单的语句中, 例如变量声明, 递增或赋值语句或函数调用。
  • 的健康)状况语句包含一个布尔表达式, 该表达式在循环的每次迭代开始时进行评估。如果条件语句的值为true, 则执行循环。
  • 的发布语句在for循环的主体之后执行。在post语句之后, 如果条件语句的值为false, 则条件语句再次求值, 然后循环结束。

例子:

// Go program to illustrate the  
// use of simple for loop 
package main
  
import "fmt"
  
// Main function
func main() {
      
     // for loop 
     // This loop starts when i = 0 
     // executes till i<4 condition is true
     // post statement is i++
     for i := 0; i < 4; i++{
       fmt.Printf( "lsbin\n" )  
     }
    
}

输出如下:

lsbin
lsbin
lsbin
lsbin

2.对于循环作为无限循环:通过从for循环中删除所有三个表达式, for循环也可用作无限循环。如果用户未在for循环中编写条件语句, 则意味着条件语句为true, 并且循环进入无限循环。

语法如下:

for{
     // Statement...
}

例子:

// Go program to illustrate the  
// use of an infinite loop 
package main
  
import "fmt"
  
// Main function
func main() {
      
     // Infinite loop
     for {
       fmt.Printf( "lsbin\n" )  
     }
    
}

输出如下:

lsbin
lsbin
lsbin
lsbin
lsbin
lsbin
lsbin
lsbin
lsbin
lsbin
...........

3. for循环, 而while循环:for循环也可以用作while循环。执行此循环, 直到给定条件为真为止。当给定条件的值为false时, 循环结束。

语法如下:

for condition{
    // statement..
}

例子:

// Go program to illustrate the  
// the for loop as while Loop
package main
  
import "fmt"
  
// Main function
func main() {
      
     // while loop
     // for loop executes till 
     // i < 3 condition is true
     i:= 0
     for i < 3 {
        i += 2
     }
   fmt.Println(i) 
}

输出如下:

4

4. for循环中的简单范围:你也可以在for循环中使用范围。

语法如下:

for i, j:= range rvariable{
   // statement..
}

这里,

  • i和j是分配迭代值的变量。它们也称为迭代变量。
  • 第二个变量j是可选的。
  • 范围表达式在循环开始前被评估一次。

例子:

// Go program to illustrate the  
// use of simple range loop 
package main
  
import "fmt"
  
// Main function
func main() {
      
     // Here rvariable is a array
     rvariable:= []string{ "GFG" , "Geeks" , "lsbin" } 
      
     // i and j stores the value of rvariable
     // i store index number of individual string and
     // j store individual string of the given array
     for i, j:= range rvariable {
        fmt.Println(i, j) 
     }
    
}

输出如下:

0 GFG
1 Geeks
2 lsbin

5.对字符串使用for循环:for循环可以遍历字符串的Unicode代码点。

语法如下:

for index, chr:= range str{
     // Statement..
}

在这里, 索引是存储UTF-8编码代码点的第一个字节的变量, 并且chr存储给定字符串的字符并str是一个字符串。

例子:

// Go program to illustrate the  
// use for loop using string
package main
  
import "fmt"
  
// Main function
func main() {
      
     // String as a range in the for loop
     for i, j:= range "XabCd" {
        fmt.Printf( "The index number of %U is %d\n" , j, i) 
     }
    
}

输出如下:

The index number of U+0058 is 0
The index number of U+0061 is 1
The index number of U+0062 is 2
The index number of U+0043 is 3
The index number of U+0064 is 4

6.对于地图:for循环可以遍历映射的键和值对。

语法如下:

for key, value := range map { 
     // Statement.. 
}

例子:

// Go program to illustrate the  
// use for loop using maps
package main
  
import "fmt"
  
// Main function
func main() {
      
     // using maps
     mmap := map[ int ]string{
         22: "Geeks" , 33: "GFG" , 44: "lsbin" , }
     for key, value:= range mmap {
        fmt.Println(key, value) 
     }
    
}

输出如下:

22 Geeks
33 GFG
44 lsbin

7.对于渠道:for循环可以遍历通道上发送的顺序值, 直到关闭为止。

语法如下:

for item := range Chnl { 
     // statements..
}

例子:

// Go program to illustrate the  
// use for loop using channel
package main
  
import "fmt"
  
// Main function
func main() {
      
     // using channel
     chnl := make(chan int )
     go func(){
         chnl <- 100
         chnl <- 1000
        chnl <- 10000
        chnl <- 100000
        close(chnl)
     }()
     for i:= range chnl {
        fmt.Println(i) 
     }
    
}

输出如下:

100
1000
10000
100000

重要事项:

  • 在for循环的三个语句中不使用括号。
  • 花括号在for循环中是必需的。
  • 左括号应与post语句所在的行相同。
  • 如果数组, 字符串, 切片或映射为空, 则for循环不会给出错误并继续其流程。换句话说, 如果数组, 字符串, 切片或映射为nil, 则for循环的迭代次数为零。

木子山

发表评论

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