在Go语言中有foreach结构吗? 我可以迭代一个片或数组使用for?
当前回答
我看到很多使用范围的例子。只要注意一下,这个范围就会创建一个你迭代的对象的副本。如果对foreach范围内的内容进行更改,则不会更改原始容器中的值,在这种情况下,您将需要一个传统的for循环,其中包含一个递增的索引和遵循索引引用。例如:
for i := 0; i < len(arr); i++ {
element := &arr[i]
element.Val = newVal
}
其他回答
我刚刚实现了这个库: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
在操场上试试这段代码。
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
}
}
我看到很多使用范围的例子。只要注意一下,这个范围就会创建一个你迭代的对象的副本。如果对foreach范围内的内容进行更改,则不会更改原始容器中的值,在这种情况下,您将需要一个传统的for循环,其中包含一个递增的索引和遵循索引引用。例如:
for i := 0; i < len(arr); i++ {
element := &arr[i]
element.Val = newVal
}
下面的例子展示了如何在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;
}
该示例遍历一个函数数组,以统一函数的错误处理。一个完整的例子是谷歌的游乐场。
PS:这也表明挂大括号对于代码的可读性来说是一个坏主意。提示:for条件刚好在action()调用之前结束。很明显,不是吗?
这可能是显而易见的,但你可以像这样内联数组:
package main
import (
"fmt"
)
func main() {
for _, element := range [3]string{"a", "b", "c"} {
fmt.Print(element)
}
}
输出:
abc
https://play.golang.org/p/gkKgF3y5nmt