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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么