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

查看:152
本文介绍了单值上下文中的多值strconv.ParseInt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

我收到了错误:

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

根据我在Internet上找到的所有内容,这意味着我忽略了ParseInt返回的两个参数,但是我使用的是err. 我知道错误可能很愚蠢,但是我刚开始学习Go,这让我很困惑.

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. I know that maybe the mistake is very stupid, but I just started to learn Go and this confused me a lot.

推荐答案

表达式uint64(...)是类型 conversion ,并且不能有多个参数(操作数),但是由于 strconv.ParseInt() 有2个返回值,基本上都是将两者都传递给类型转换,这是无效的.

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.

相反,这样做:

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

请注意,基数不能大于36,因此在传递64作为基数时,肯定会出现错误.

Note that the base cannot be greater than 36, so you'll definitely get an error as you're passing 64 as the base.

或使用 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

(这里我使用的是有效的16基数.使用您必须使用的任何内容.)

(Here I used a valid, 16 base. Use whatever you have to.)

另请参阅相关问题和解答:转到:多值在单值上下文中

Also see related question+answer: Go: multiple value in single-value context

这篇关于单值上下文中的多值strconv.ParseInt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆