Go的范围可以在地图和切片上迭代,但我想知道是否有一种方法可以在数字范围上迭代,就像这样:
for i := range [1..10] {
fmt.Println(i)
}
或者在Go中是否有一种方法来表示整数的范围,就像Ruby对range类所做的那样?
Go的范围可以在地图和切片上迭代,但我想知道是否有一种方法可以在数字范围上迭代,就像这样:
for i := range [1..10] {
fmt.Println(i)
}
或者在Go中是否有一种方法来表示整数的范围,就像Ruby对range类所做的那样?
当前回答
你也可以退房 github.com/wushilin/stream
它是一个类似java.util.stream的惰性流概念。
// It doesn't really allocate the 10 elements.
stream1 := stream.Range(0, 10)
// Print each element.
stream1.Each(print)
// Add 3 to each element, but it is a lazy add.
// You only add when consume the stream
stream2 := stream1.Map(func(i int) int {
return i + 3
})
// Well, this consumes the stream => return sum of stream2.
stream2.Reduce(func(i, j int) int {
return i + j
})
// Create stream with 5 elements
stream3 := stream.Of(1, 2, 3, 4, 5)
// Create stream from array
stream4 := stream.FromArray(arrayInput)
// Filter stream3, keep only elements that is bigger than 2,
// and return the Sum, which is 12
stream3.Filter(func(i int) bool {
return i > 2
}).Sum()
希望这能有所帮助
其他回答
以下是一个紧凑的动态版本,不依赖于iter(但工作方式类似):
package main
import (
"fmt"
)
// N is an alias for an unallocated struct
func N(size int) []struct{} {
return make([]struct{}, size)
}
func main() {
size := 1000
for i := range N(size) {
fmt.Println(i)
}
}
通过一些调整,大小可以是uint64类型(如果需要),但这是要点。
如果你只是想在一个w/o范围内使用和索引或其他东西进行迭代,这个代码样本对我来说很好。不需要额外的声明,没有_。不过,还没有检查性能。
for range [N]int{} {
// Body...
}
附注:在戈朗的第一天。如果这是一种错误的方法,请进行批评。
问题不在于范围,而在于如何计算切片的末端。 对于固定的数字10,简单的for循环是可以的,但是对于像bfl.Size()这样的计算大小,你在每次迭代时都得到一个函数调用。大于int32的简单范围会有所帮助,因为这只计算bfl.Size()一次。
type BFLT PerfServer
func (this *BFLT) Call() {
bfl := MqBufferLCreateTLS(0)
for this.ReadItemExists() {
bfl.AppendU(this.ReadU())
}
this.SendSTART()
// size := bfl.Size()
for i := int32(0); i < bfl.Size() /* size */; i++ {
this.SendU(bfl.IndexGet(i))
}
this.SendRETURN()
}
下面是一个使用iter包比较Go for语句和ForClause以及Go range语句的基准测试。
iter_test.go
package main
import (
"testing"
"github.com/bradfitz/iter"
)
const loops = 1e6
func BenchmarkForClause(b *testing.B) {
b.ReportAllocs()
j := 0
for i := 0; i < b.N; i++ {
for j = 0; j < loops; j++ {
j = j
}
}
_ = j
}
func BenchmarkRangeIter(b *testing.B) {
b.ReportAllocs()
j := 0
for i := 0; i < b.N; i++ {
for j = range iter.N(loops) {
j = j
}
}
_ = j
}
// It does not cause any allocations.
func N(n int) []struct{} {
return make([]struct{}, n)
}
func BenchmarkIterAllocs(b *testing.B) {
b.ReportAllocs()
var n []struct{}
for i := 0; i < b.N; i++ {
n = iter.N(loops)
}
_ = n
}
输出:
$ go test -bench=. -run=.
testing: warning: no tests to run
PASS
BenchmarkForClause 2000 1260356 ns/op 0 B/op 0 allocs/op
BenchmarkRangeIter 2000 1257312 ns/op 0 B/op 0 allocs/op
BenchmarkIterAllocs 20000000 82.2 ns/op 0 B/op 0 allocs/op
ok so/test 7.026s
$
Mark Mishyn建议使用slice,但是没有理由使用make和use in来创建数组,因为通过literal创建的数组可以被使用,而且它更短
for i := range [5]int{} {
fmt.Println(i)
}