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