strconv.Atoi() throwing error when given a string
The error is telling you that the two return values from strconv.Atoi
(int
and error
) are used in a single value context (the assignment to time
). Change the code to:
time, err := strconv.Atoi(times)
if err != nil {
// Add code here to handle the error!
}
Use the blank identifier to ignore the error
return value:
time, _ := strconv.Atoi(times)
Although this has been answered and the question is quite old, I thought it would be nice to complement the accepted answer.
When a function returns multiple values as in the question, if any of the value is not needed then they can be discarded by using the blank identifier _
as in the following.
num, _ := strconv.Atoi(numAsString)
This would store the converted number in num
but discard the error by assigning it to the blank identifier.
But do note that once a value is assigned to _
it cannot be referenced again. i.e.
num, _ := strconv.Atoi(numAsString)
fmt.Println(_) // won't compile. cannot reference _