Go的range可以迭代地遍历maps和slices,但我想知道是否有一种方法可以迭代数值范围,就像这样:
for i := range [1..10] {
fmt.Println(i)
}
在Go语言中是否有一种像Ruby的Range类那样表示整数范围的方法?
我对这个提案能够被纳入语言中持乐观态度。如果 n 是整数类型,那么
for x := range n { ... }
将完全等同于 forx := T(0); x < n; x++ { ... }
,其中 T 是 n 的类型(假设在循环体中未修改 x)。
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()
}
package main
import "fmt"
func main() {
nums := []int{2, 3, 4}
for _, num := range nums {
fmt.Println(num, sum)
}
}