📜  如何在 Golang 中使用 for 和 foreach 循环?

📅  最后修改于: 2021-10-25 02:53:34             🧑  作者: Mango

Golang 中只有一种循环结构,那就是for循环。 Golang 中的 for 循环包含三个组件,必须用分号 (;) 分隔,它们是:

  • 初始化语句:在第一次迭代之前执行。例如我:= 0
  • 条件表达式:在每次迭代之前执行。例如我 < 5
  • Post 语句:在每次迭代结束时执行。例如我++

不需要任何括号来包围这三个组件,但要定义一个块,我们必须使用大括号{ }

for i := 0 ; i < 5 ; i++{
         // statements to execute......
}

初始化和 post 语句是可选的。

i:=0
for ; i < 5;{
i++
}

您可以在 Golang 中将for 循环用作 while 循环。只需删除所有分号。

i := 0
for i < 5 {
i++
}

无限循环:如果没有条件语句,循环就变成无限循环。

for {
}

例子:

C
package main
 
import "fmt"
 
// function to print numbers 0
// to 9 and print the sum of 0 to 9
func main() {
 
    // variable to store the sum
    sum := 0
 
    // this is a for loop which runs from 0 to 9
    for i := 0; i < 10; i++ {
 
        // printing the value of
        // i : the iterating variable
        fmt.Printf("%d ", i)
 
        // calculating the sum
        sum += i
    }
    fmt.Printf("\nsum = %d", sum)
}


C
package main
 
import "fmt"
 
// Driver function to show the
// use of for and range together
func main() {
 
    // here we used a map of integer to string
    mapp := map[int]string{1: "one", 2: "two", 3: "three"}
 
    // integ act as keys of mapp
    // spell act as the values of
    // mapp which is mapped to integ
 
    for integ, spell := range mapp {
 
        // using integ and spell as
        // key and value of the map
        fmt.Println(integ, " = ", spell)
    }
}


C
package main
 
import "fmt"
 
// Driver function to show the
// use of for and range together
func main() {
 
    // declaring an array of integers
    arra := []int{1, 2, 3, 4}
 
    // traversing through the array
    for index, itr := range arra {
     
        // the key or value variables
        // used in for syntax
        // depends on the container.
        // If its an array or list,
        // the key refers to the index...
        fmt.Print(index, " : ", itr, "\n")
    }
 
    // if we use only one
    // variable in the for loop,
    // it by default refers to
    // the value in the container.
    for itr := range arra {
 
        fmt.Print(it, " ")
    }
}


输出:

0 1 2 3 4 5 6 7 8 9 
sum = 45

在 Golang 中没有 foreach 循环取而代之, for循环可以用作“ foreach ”。有一个关键字range ,您可以将forrange组合在一起,并可以选择在循环中使用键值。
句法:

for ,  := range {

}

这里,

  • keyvalue :它可以是您想要选择的任何变量。
  • container :它可以是任何变量,可以是数组、列表、地图等。

示例 1:

C

package main
 
import "fmt"
 
// Driver function to show the
// use of for and range together
func main() {
 
    // here we used a map of integer to string
    mapp := map[int]string{1: "one", 2: "two", 3: "three"}
 
    // integ act as keys of mapp
    // spell act as the values of
    // mapp which is mapped to integ
 
    for integ, spell := range mapp {
 
        // using integ and spell as
        // key and value of the map
        fmt.Println(integ, " = ", spell)
    }
}

输出:

1  =  one
2  =  two
3  =  three

示例 2:

C

package main
 
import "fmt"
 
// Driver function to show the
// use of for and range together
func main() {
 
    // declaring an array of integers
    arra := []int{1, 2, 3, 4}
 
    // traversing through the array
    for index, itr := range arra {
     
        // the key or value variables
        // used in for syntax
        // depends on the container.
        // If its an array or list,
        // the key refers to the index...
        fmt.Print(index, " : ", itr, "\n")
    }
 
    // if we use only one
    // variable in the for loop,
    // it by default refers to
    // the value in the container.
    for itr := range arra {
 
        fmt.Print(it, " ")
    }
}

输出:

0 : 1
1 : 2
2 : 3
3 : 4
1 2 3 4