英文:
Iterate on a golang array/slice without using for statement
问题
在不使用for
语句的情况下,是否可以迭代Go语言的数组/切片?
英文:
Is it possible to iterate on a golang array/slice without using 'for' statement?
答案1
得分: 9
你可以使用goto
语句(不推荐使用)。
package main
import (
"fmt"
)
func main() {
my_slice := []string {"a", "b", "c", "d"}
index := 0
back:
if index < len(my_slice) {
fmt.Println(my_slice[index])
index += 1
goto back
}
}
请注意,使用goto
语句并不被推荐。
英文:
You could use goto
statement (not recommended).
package main
import (
"fmt"
)
func main() {
my_slice := []string {"a", "b", "c", "d"}
index := 0
back:
if index < len(my_slice) {
fmt.Println(my_slice[index])
index += 1
goto back
}
}
答案2
得分: 2
如@LeoCorrea所提到的,您可以使用递归函数来迭代一个切片。尾递归可以避免@vutran提到的堆栈溢出问题。
package main
import "fmt"
func num(a []string, i int) {
if i >= len(a) {
return
} else {
fmt.Println(i, a[i]) //0 a 1 b 2 c
i += 1
num(a, i) //尾递归
}
}
func main() {
a := []string{"a", "b", "c"}
i := 0
num(a, i)
}
一个可能更易读但不太纯粹的例子可以使用匿名函数。请参阅https://play.golang.org/p/Qen6BKviWuE。
英文:
As mentioned by @LeoCorrea you could use a recursive function to iterate over a slice. A tail recursion could prevent the stack overflow mentioned by @vutran.
package main
import "fmt"
func num(a []string, i int) {
if i >= len(a) {
return
} else {
fmt.Println(i, a[i]) //0 a 1 b 2 c
i += 1
num(a, i) //tail recursion
}
}
func main() {
a := []string{"a", "b", "c"}
i := 0
num(a, i)
}
A possibly more readable but less pure example could use an anonymous function. See https://play.golang.org/p/Qen6BKviWuE.
答案3
得分: 1
你可以编写一个递归函数来迭代遍历切片,但是为什么不想使用for
循环呢?
英文:
You could write a recursive function to iterate over the slice but why would you want to not use a for
loop?
答案4
得分: 0
Go语言没有像for
或while
这样不同的循环关键字,它只有for
关键字,但有几种不同的形式。
英文:
Go doesn't have different loop keywords like for
or while
, it just has for
which has a few different forms
答案5
得分: -2
我不明白为什么你想这样做,但是这里有一个不使用for
循环的代码示例。
package main
import "fmt"
type P struct {
Next *P
}
func (p *P) Iterate() *P {
if p.Next != nil {
fmt.Println("Saw another P")
return p.Next.Iterate()
}
return nil
}
func main() {
var z []*P
z = append(z, &P{})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z[len(z)-1].Iterate()
}
请注意,虽然它包含了一个切片,但切片本身的属性完全没有被使用。
英文:
I also don't understand why you'd want to do this, but here is a code sample using no for
loops.
package main
import "fmt"
type P struct {
Next *P
}
func (p *P) Iterate() *P {
if p.Next != nil {
fmt.Println("Saw another P")
return p.Next.Iterate()
}
return nil
}
func main() {
var z []*P
z = append(z, &P{})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z = append(z, &P{Next: z[len(z)-1]})
z[len(z)-1].Iterate()
}
https://play.golang.org/p/CMSp6M00kR
Please note that, while it contains a slice as requested, the properties of the slice itself go completely unused.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论