duanfan1965 2017-11-03 09: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 10: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部