2014-10-03 31 views
1

这是我的测试函数。GoLang的Int溢出

if -1 != cmp(2<<32, keys[2].Distance(keys[5])) { 
     t.Errorf("2<<32 should be smaller") 
    } 

它导致folllowing错误

不断8589934592溢出诠释

是否有可能使一个32位系统上这项工作?

编辑:还这是比较关键

距离函数
// Distance returns the distance metric in this key space 
func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int { 
    // XOR the keys 
    k3 := XOR(k1.Bytes, k2.Bytes) 

    // interpret it as an integer 
    dist := big.NewInt(0).SetBytes(k3) 
    return dist 
} 
+1

你确定你使用的golang版本是64位吗?根据文档上的64位去int应该是64位 – 2014-10-03 19:54:02

+0

我刚刚检查了一个64位版本去,并没有问题,2 << 32作为int – 2014-10-03 19:56:35

+0

@sourcey运行'去版本',输出是什么你的机器。 – nos 2014-10-03 19:58:40

回答

1

确保你在一个64位INT的工作,最好的办法是使用uint64

type Key int64 // or uint64 

假设,以确保尺寸键被定义为int,否则将所有功能签名从int更改为int64