doujiao9426 2017-11-06 13:22
浏览 121
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • drgweamoi473182981 2017-11-06 13:24
    关注

    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.

    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.

    Or use strconv.ParseUint() which will return you an uint value right away:

    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.)

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题