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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)