duanfu1942 2015-04-28 21:45 采纳率: 100%
浏览 95
已采纳

Go中的大int范围

Is there a way to loop over intervals between two big int values, x and y, in Go?

for i: = x; i < y; i++ {
    // do something
}
  • 写回答

1条回答 默认 最新

  • dqgo99177 2015-04-28 21:49
    关注

    Working with big numbers can be kind of clunky because you need to create a big.Int for constants. Other than that, it is a straight forward replacement of each segment of the for statement to one made to deal with big ints.

    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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度