strconv.ParseInt: 解析”18446744073709551615″时出错:值超出范围

huangapple go评论68阅读模式
英文:

strconv.ParseInt: parsing "18446744073709551615": value out of range

问题

使用strconv.ParseInt函数解析uint64的最大值时,出现错误是正常行为吗?

我使用以下代码进行解析:

i, err := strconv.ParseInt("18446744073709551615", 10, 64)
fmt.Println(i, err)

我得到了一个错误:"strconv.ParseInt: parsing "18446744073709551615": value out of range",但是uint64的最大允许值是:18446744073709551615。

你能解释这种行为吗?

你可以在以下链接中查看相关代码:

https://golang.org/src/builtin/builtin.go?s=1026:1044#L26

英文:

Is it a normal behavior when parsing uint64 max value with strconv.ParseInt?

i, err := strconv.ParseInt("18446744073709551615", 10, 64)
fmt.Println(i, err)

I got an error: "strconv.ParseInt: parsing "18446744073709551615": value out of range", when maximum allowed value for uint64 is: 18446744073709551615

Can you explain such behavior?

https://golang.org/src/builtin/builtin.go?s=1026:1044#L26

答案1

得分: 7

调用ParseUint函数来解析无符号整数。

ParseInt函数用于解析有符号整数。最大的有符号整数是9223372036854775807。

英文:

Call ParseUint to parse an unsigned integer.

The ParseInt function parses signed integers. The maximum signed integer is 9223372036854775807.

答案2

得分: 5

根据评论,我将您的代码重新编写如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {

	i, err := strconv.ParseUint("18446744073709551615", 10, 64)
	fmt.Println(i, err)

}

输出结果:

18446744073709551615 <nil>
英文:

Based the comments ,I reproduced your code as follows:

package main

import (
	&quot;fmt&quot;
	&quot;strconv&quot;
)

func main() {

	i, err := strconv.ParseUint(&quot;18446744073709551615&quot;, 10, 64)
	fmt.Println(i, err)

}

Output:

18446744073709551615 &lt;nil&gt;

huangapple
  • 本文由 发表于 2021年6月11日 02:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/67926756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定