Go语言中是否有foreach循环?

774

Go语言中是否有foreach结构?我能否使用for来迭代切片或数组?


1
请查看:http://groups.google.com/group/golang-nuts/browse_thread/thread/e2966ccdfe910e14?pli=1 - Kakashi
2
Go教程的“类型插曲”部分(朝向其结尾)也提到了在for循环中使用range的用法。 - kostix
9个回答

1116

来自带有range子句的for语句

使用“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
}

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


27
在这个例子中,“element”是元素的(一份副本)——它不是元素本身。虽然你可以给“element”赋值,但这不会影响基础序列。 - Brent Bradburn
我知道在Python和C中,使用下划线作为本地化函数很常见(即[gettext](https://docs.python.org/3/library/gettext.html#localizing-your-module))。在Go中使用下划线会有任何问题吗? Go是否甚至使用相同的本地化库? - Sergiy Kolodyazhnyy
4
Py文档表示:“(gettext)函数通常在本地命名空间中别名为_()”,这仅是约定俗成,它不是本地化库的一部分。下划线 _ 是一个有效的标签,而且在Go(以及Python、Scala和其他语言)中按惯例将返回值分配给 _ 不会使用的变量。在此示例中,_ 的范围限制在 for 循环的主体中。如果您有一个包范围的函数 _,则它将在 for 循环的作用域内被遮蔽。有几个本地化软件包,我没有看到任何一个使用 _ 作为函数名。 - Davos
请参见Moshe Revah的答案,了解更多for...range的用法示例。包括切片、映射和通道。 - kapad

227

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 {}

遍历一个通道(channel)

for v := range theChan {}

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

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

22
尽管原帖仅询问切片用法,但我更喜欢这个答案,因为大多数人最终也需要使用其他用法。 - domoarigato
4
关于 chan 的使用,有一个重要的区别:通过对通道进行迭代,在某个时间点,如果写入者关闭了该通道,那么迭代会正常退出。在使用 _equivalent_ 中的 for {v := <-theChan} 时,它将不会在通道关闭时退出。您可以通过第二个 ok 返回值来测试这一点。TOUR EXAMPLE - colm.anseo
阅读时也有同样的想法,for { ... }代表无限循环。 - Levite
如果没有键和值,只是想运行特定的长度,而不对数组做任何操作,怎么样?例如:for _ := range slice{} - Duong Phan

24

以下是如何在Go中使用foreach的示例代码:

package main

import (
    "fmt"
)

func main() {

    arrayOne := [3]string{"Apple", "Mango", "Banana"}

    for index,element := range arrayOne{

        fmt.Println(index)
        fmt.Println(element)

    }

}

这是一个运行示例 https://play.golang.org/p/LXptmH4X_0


2
有时候最简单的例子是最有用的。谢谢!我并不反对其他评论者提供的最深奥的答案——它们确实展示了非常惯用的Go编程的复杂性,以至于它们变得……难以阅读和理解——但我更喜欢你的答案:它用最简单的例子直接到达核心(这个例子有效且显而易见为什么它有效)。 - Gwyneth Llewelyn

16

是的,范围

for 循环的范围形式遍历切片或映射。

当在切片上进行范围时,每次迭代会返回两个值。第一个是索引,第二个是该索引处元素的副本。

示例:

package main

import "fmt"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
    for i, v := range pow {
        fmt.Printf("2**%d = %d\n", i, v)
    }

    for i := range pow {
        pow[i] = 1 << uint(i) // == 2**i
    }
    for _, value := range pow {
        fmt.Printf("%d\n", value)
    }
}
  • 您可以通过将值赋给_来跳过索引或值。
  • 如果您只想要索引,请完全省略,值。

13
下面的示例展示了如何在for循环中使用range运算符来实现foreach循环。
func PrintXml (out io.Writer, value interface{}) error {
    var data []byte
    var err error

    for _, action := range []func() {
        func () { data, err = xml.MarshalIndent(value, "", "  ") },
        func () { _, err = out.Write([]byte(xml.Header)) },
        func () { _, err = out.Write(data) },
        func () { _, err = out.Write([]byte("\n")) }} {
        action();
        if err != nil {
            return err
        }
    }
    return nil;
}

该示例遍历函数数组,以统一函数的错误处理。完整示例位于Google的playground上。

附注:它还显示悬挂的大括号不利于代码的可读性。提示:for循环条件在action()调用之前就结束了。显然,不是吗?


3
添加一个逗号,就可以更清楚地看出for循环的条件在哪里结束了:http://play.golang.org/p/pcRg6WdxBd - 这其实是我第一次发现反对go fmt样式的论据,谢谢! - topskip
@topskip 两个都是go fmt有效的;只需选择最好的一个 :) - Filip Haglund
@FilipHaglund 这不是有效性的问题。问题在于,在上述特定情况下,我认为for条件结束的位置更清晰明了。 - topskip
12
我认为,对于这个问题来说,这个答案过于复杂了。 - AndreasHassing
@AndreasHassing 如何在不引入冗余的情况下完成它? - ceving

12

实际上,您可以使用for range针对您的类型而不是引用其返回值来使用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


3
知道这个信息是好的,但在大多数情况下并没有什么用处。 - Sridhar
同意@Sridhar,这很小众。 - robstarbuck

1

我看到很多使用 range 的示例。提醒一下,range 会创建一个您正在迭代的任何内容的副本。如果您在 foreach range 中更改内容,则不会更改原始容器中的值,在这种情况下,您需要使用传统的 for 循环,带有递增的索引和解除引用的索引引用。例如:

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

1
这可能很显然,但您可以像这样内联数组:
package main

import (
    "fmt"
)

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

输出:
abc

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


0

我刚刚实现了这个库:https://github.com/jose78/go-collection

这是使用Foreach循环的示例:

package main

import (
    "fmt"

    col "github.com/jose78/go-collection/collections"
)

type user struct {
    name string
    age  int
    id   int
}

func main() {
    newList := col.ListType{user{"Alvaro", 6, 1}, user{"Sofia", 3, 2}}
    newList = append(newList, user{"Mon", 0, 3})

    newList.Foreach(simpleLoop)

    if err := newList.Foreach(simpleLoopWithError); err != nil{
        fmt.Printf("This error >>> %v <<< was produced", err )
    }
}

var simpleLoop col.FnForeachList = func(mapper interface{}, index int) {
    fmt.Printf("%d.- item:%v\n", index, mapper)
}


var simpleLoopWithError col.FnForeachList = func(mapper interface{}, index int) {
    if index > 1{
        panic(fmt.Sprintf("Error produced with index == %d\n", index))
    }
    fmt.Printf("%d.- item:%v\n", index, mapper)
}

执行的结果应该是:

0.- item:{Alvaro 6 1}
1.- item:{Sofia 3 2}
2.- item:{Mon 0 3}
0.- item:{Alvaro 6 1}
1.- item:{Sofia 3 2}
Recovered in f Error produced with index == 2

ERROR: Error produced with index == 2
This error >>> Error produced with index == 2
 <<< was produced

在playGrounD中尝试这段代码


这是一个for-each循环吗?它不只是列表处理吗?你能在你的回答中详细解释一下吗?(但请不要包含“编辑:”,“更新:”或类似内容 - 问题/答案应该看起来像今天写的。) - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接