Go 中的大整数范围

Big int ranges in Go

有没有办法在 Go 中循环两个 big 整数值 x 和 y 之间的间隔?

for i: = x; i < y; i++ {
    // do something
}

使用大数字可能有点笨拙,因为您需要为常量创建 big.Int。除此之外,它是将 for 语句的每个部分直接替换为处理大整数的部分。

http://play.golang.org/p/pLSd8yf9Lz

package main

import (
    "fmt"
    "math/big"
)

var one = big.NewInt(1)

func main() {
    start := big.NewInt(1)
    end := big.NewInt(5)
    // i must be a new int so that it does not overwrite start
    for i := new(big.Int).Set(start); i.Cmp(end) < 0; i.Add(i, one) {
        fmt.Println(i)
    }
}