在不使用for语句的情况下迭代遍历golang数组/切片。

huangapple go评论73阅读模式
英文:

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 (
	&quot;fmt&quot;
)

func main() {
	my_slice := []string {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;}

	index := 0

back:
	if index &lt; 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 &quot;fmt&quot;

func num(a []string, i int) {
	if i &gt;= 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{&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}
	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语言没有像forwhile这样不同的循环关键字,它只有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 &quot;fmt&quot;

type P struct {
	Next *P
}

func (p *P) Iterate() *P {
	if p.Next != nil {
		fmt.Println(&quot;Saw another P&quot;)
		return p.Next.Iterate()
	}

	return nil
}

func main() {
	var z []*P

	z = append(z, &amp;P{})
	z = append(z, &amp;P{Next: z[len(z)-1]})
	z = append(z, &amp;P{Next: z[len(z)-1]})
	z = append(z, &amp;P{Next: z[len(z)-1]})
	z = append(z, &amp;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.

huangapple
  • 本文由 发表于 2015年12月17日 11:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/34326007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定