Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 戈朗溢出int64_Go_Biginteger_Int64 - Fatal编程技术网

Go 戈朗溢出int64

Go 戈朗溢出int64,go,biginteger,int64,Go,Biginteger,Int64,我试图使用这段代码,但给了我一个错误:常量10000000000000000000000溢出int64 我怎样才能解决这个问题 // Initialise big numbers with small numbers count, one := big.NewInt(100000000000000000000000), big.NewInt(1) 例如: count,one := new(big.Int), big.NewInt(1) count.SetString("100000000000

我试图使用这段代码,但给了我一个错误:常量10000000000000000000000溢出int64

我怎样才能解决这个问题

// Initialise big numbers with small numbers
count, one := big.NewInt(100000000000000000000000), big.NewInt(1)
例如:

count,one := new(big.Int), big.NewInt(1)
count.SetString("100000000000000000000000",10)
链接:

它不起作用,因为在引擎盖下,big.NewInt实际上分配了一个int64。要分配到big.NewInt中的数字需要超过64位才能存在,因此失败

但是,如果您想在MaxInt64下面添加两个大的数字,您可以这样做!即使总和大于MaxInt64。下面是我刚刚写的一个例子():

其结果是:

count:     10000000000000000000
max int64:  9223372036854775807
现在,如果您仍然对NewInt如何在引擎盖下工作感到好奇,那么下面是您正在使用的函数,摘自Go的文档:

// NewInt allocates and returns a new Int set to x.
func NewInt(x int64) *Int {
    return new(Int).SetInt64(x)
}
资料来源:

// NewInt allocates and returns a new Int set to x.
func NewInt(x int64) *Int {
    return new(Int).SetInt64(x)
}