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.
是否有可能在Go语言中迭代数组索引并且不是选择所有索引而是抛出一些句点(例如,1,2,3)。
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
}
2 个解决方案
#1
17
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,
如果你想要除索引之外的i值,或者如果你想跳过索引,
for i := 1; i < len(array); i += 4 {
v := array[i]
}
?
?
#2
0
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:
您正在寻找Golang中不存在的抽象。 Go设计“简单”。当然,简单本身就是一个非常相对和主观的术语。例如,以下内容对某些人来说很简单:
// 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 < 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的大多数工程权衡在简单性和速度方面都是错误的。
Go does let you build such an abstraction though, with a little effort.
Go确实让你通过一点点努力来构建这样的抽象。
#1
17
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,
如果你想要除索引之外的i值,或者如果你想跳过索引,
for i := 1; i < len(array); i += 4 {
v := array[i]
}
?
?
#2
0
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:
您正在寻找Golang中不存在的抽象。 Go设计“简单”。当然,简单本身就是一个非常相对和主观的术语。例如,以下内容对某些人来说很简单:
// 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 < 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的大多数工程权衡在简单性和速度方面都是错误的。
Go does let you build such an abstraction though, with a little effort.
Go确实让你通过一点点努力来构建这样的抽象。