简体   繁体   English

单值上下文中的多值strconv.ParseInt()

[英]multiple-value strconv.ParseInt() in single-value context

I have the following code: 我有以下代码:

var i2 uint64;
var err error;
i2, err = uint64(strconv.ParseInt(scanner.Text(), 64, 64));

And I received the error: 我收到了错误:

multiple-value strconv.ParseInt() in single-value context

According to everything I found on the Internet, this means that I am ignoring the two parameters returned by ParseInt, but I am using err. 根据我在互联网上找到的所有内容,这意味着我忽略了ParseInt返回的两个参数,但我使用的是错误的。 I know that maybe the mistake is very stupid, but I just started to learn Go and this confused me a lot. 我知道也许错误是非常愚蠢的,但我刚开始学习Go,这让我很困惑。

The expression uint64(...) is a type conversion , and it cannot have multiple arguments (operands), but since strconv.ParseInt() has 2 return values, you're basically passing both to the type conversion, which is not valid. 表达式uint64(...)是一个类型转换 ,它不能有多个参数(操作数),但由于strconv.ParseInt()有2个返回值,你基本上都将两者都传递给了类型转换,这是无效的。

Instead do this: 而是这样做:

i, err := strconv.ParseInt(scanner.Text(), 64, 64)
// Check err
i2 := uint64(i)

Note that the base cannot be greater than 36 , so you'll definitely get an error as you're passing 64 as the base. 请注意,基数不能大于36 ,所以当你传递64作为基数时,你肯定会得到一个错误。

Or use strconv.ParseUint() which will return you an uint value right away: 或者使用strconv.ParseUint() ,它会立即返回一个uint值:

i, err := strconv.ParseUint(scanner.Text(), 16, 64)
// i is of type uint64, and ready to be used if err is nil

(Here I used a valid, 16 base. Use whatever you have to.) (这里我使用了有效的16碱基。使用你需要的任何东西。)

Also see related question+answer: Go: multiple value in single-value context 另请参阅相关问题+答案: Go:单值上下文中的多个值

multiple-value strconv.ParseInt() in single-value context 单值上下文中的多值strconv.ParseInt()

ParseInt returns 2 values: integer and error. ParseInt返回2个值:整数和错误。 So you cannot use them in function argument where only one value allowed. 所以你不能在函数参数中使用它们,只允许一个值。 You may first get value and error then use value in next operations. 您可能首先获得valueerror然后在下一个操作中使用value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM