duanfan1965 2017-11-03 17:39
浏览 46
已采纳

Golang:有没有一种方法可以在一行中修改其中一个多值返回参数?

I am trying something relatively simple in Go - convert a string to integer and then double it:

myInt, _ := strconv.Atoi(args[1])
doubleArg := myInt * 2

Since Atoi() returns two parameters (the integer and err), I am using myInt, _ := to retrieve the value of the integer. I would like to double it (hence the 2nd line) but can't do all in one line:

myInt, _ := strconv.Atoi(args[1]) * 2

gives me:

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

However, from my experience with most other languages it seems like a lot of boilerplate to have to do this in two lines. Is this just a limitation I'll have to deal with, or is there a better way to write my code?

  • 写回答

1条回答 默认 最新

  • dongyan7172 2017-11-03 18:53
    关注

    Two lines of code is not really that much. But if you have to do the same thing many times in your code, you might as well write your own version of the conversion then multiplication function. This function can do error checking, and the real work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?