在Go语言中有foreach结构吗? 我可以迭代一个片或数组使用for?


当前回答

对于带有range子句的语句:

带有“range”子句的“for”语句将遍历所有条目 数组、切片、字符串或映射,或在通道上接收的值。 对于每个条目,它将迭代值赋给相应的迭代 变量,然后执行块。

举个例子:

for index, element := range someSlice {
    // index is the index where we are
    // element is the element from someSlice for where we are
}

如果你不关心索引,你可以使用_:

for _, element := range someSlice {
    // element is the element from someSlice for where we are
}

下划线_是空白标识符,一个匿名占位符。

其他回答

对于带有range子句的语句:

带有“range”子句的“for”语句将遍历所有条目 数组、切片、字符串或映射,或在通道上接收的值。 对于每个条目,它将迭代值赋给相应的迭代 变量,然后执行块。

举个例子:

for index, element := range someSlice {
    // index is the index where we are
    // element is the element from someSlice for where we are
}

如果你不关心索引,你可以使用_:

for _, element := range someSlice {
    // element is the element from someSlice for where we are
}

下划线_是空白标识符,一个匿名占位符。

我看到很多使用范围的例子。只要注意一下,这个范围就会创建一个你迭代的对象的副本。如果对foreach范围内的内容进行更改,则不会更改原始容器中的值,在这种情况下,您将需要一个传统的for循环,其中包含一个递增的索引和遵循索引引用。例如:

for i := 0; i < len(arr); i++ {
    element := &arr[i]
    element.Val = newVal
}

Go有一个类似foreach的语法。它支持数组/切片、映射和通道。

迭代数组或切片:

// index and value
for i, v := range slice {}

// index only
for i := range slice {}

// value only
for _, v := range slice {}

迭代一个地图:

// key and value
for key, value := range theMap {}

// key only
for key := range theMap {}

// value only
for _, value := range theMap {}

遍历一个通道:

for v := range theChan {}

遍历一个通道相当于从一个通道接收直到它关闭:

for {
    v, ok := <-theChan
    if !ok {
        break
    }
}

实际上,你可以对你的类型使用for range而不引用它的返回值:

arr := make([]uint8, 5)
i,j := 0,0
for range arr {
    fmt.Println("Array Loop", i)
    i++
}

for range "bytes" {
    fmt.Println("String Loop", j)
    j++
}

https://play.golang.org/p/XHrHLbJMEd

这可能是显而易见的,但你可以像这样内联数组:

package main

import (
    "fmt"
)

func main() {
    for _, element := range [3]string{"a", "b", "c"} {
        fmt.Print(element)
    }
}

输出:

abc

https://play.golang.org/p/gkKgF3y5nmt