Loop over array in Go language

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

Loop over array in Go language

问题

在Go语言中,可以迭代数组索引,并选择一些特定的索引进行迭代,而不是全部索引。例如:

for i, v := range array {
    // 使用 i,v 进行一些操作
}

这段代码会迭代数组中的所有索引。

你想知道的是是否有可能实现以下类似的功能:

for i:=1, v := range array {
    // 使用 i,v 进行一些操作
    i += 4
}

很抱歉,Go语言的for循环语法不支持这种写法。在for循环中,迭代变量的增量只能是固定的,无法在循环体内部进行动态修改。如果你想要实现类似的功能,可以考虑使用普通的for循环,并在循环体内部手动控制索引的增量。

英文:

Is it possible to iterate over array indices in Go language and choose not all indices but throw some period (1, 2, 3 for instance.

For example,

for i, v := range array {
//do something with i,v
}

iterates over all indices in the array

What I want to know is there any chance to have something like that

for i:=1, v := range array {
//do something with i,v
i += 4
}

答案1

得分: 17

以下是代码的翻译:

如果你想要获取除了索引之外的i值,或者想要跳过索引,可以使用以下代码:

i := 1
for _, v := range array {
    // 做一些操作
    i += 4
}

或者可以使用以下代码:

for i := 1; i < len(array); i += 4 {
    v := array[i]
}

这样做可以达到你的目的吗?

英文:

What's wrong with

i := 1
for _, v := range array {
    // do something
    i += 4
}

if you want i-values other than indices, or if you want to skip the indices,

for i := 1; i &lt; len(array); i += 4 {
    v := array[i]
}

?

答案2

得分: 0

你正在寻找一个在Golang中不存在的抽象概念。Go语言的设计理念是“简单”。当然,简单本身是一个非常相对和主观的术语。例如,对于某些人来说,以下代码可能很简单:

// 这不是真正的Go代码
for index, value := range(array, stride = 4, start = 1) {
...
}

这是因为它告诉编译器要做什么,而不是如何做 - 定义性的抽象 - 如何做可能会改变,而不改变要做什么。但是如何做是抽象的。正因为这个原因,其他一些人可能更喜欢:

// 这是真正的Go代码
start := 1 // 我们从1开始,而不是从0开始,只是为了好玩
stride := 4 // 在当前元素和下一个感兴趣的元素之间跳过3个
for i: = start; i < len(array); i += stride {
...
}

这段代码告诉你如何做某件事,并且作为副作用,你应该理解正在做什么。抽象程度较低 - 但这是在速度和简单性之间进行的工程权衡。Go语言的大多数工程权衡都偏向于简单和速度。

不过,Go语言确实允许你通过一些努力来构建这样的抽象。

英文:

You're looking for an abstraction that does not exist in Golang. Go is "simple" by design. Of course simple itself is a very relative and subjective term. For example, the following would be simple to some:

// This is not real Go code
for index, value := range(array, stride = 4, start = 1) {
        ...
}

That's because it tells the compiler what to do, not how to do it - definitive abstraction - the how could change without the what changing. But the how is abstracted. Precisely for that reason, some others would prefer:

// This *is* real Go code
start  := 1   // we start not at 0 but at one, just for fun
stride := 4   // we skip 3 between this and the next one of interest
for i: = start; i &lt; len(array); i += stride {
        ...
}

This code tells you how something is being done, and as a side-effect, you ought to understand what's being done. Little abstraction - but that's an engineering trade-off for being both somewhat fast, and somewhat simple. Most of Go's engineering trade-offs err on the side of simplicity and speed.

Go does let you build such an abstraction though, with a little effort.

huangapple
  • 本文由 发表于 2013年9月2日 14:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/18566811.html
匿名

发表评论

匿名网友

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

确定