3

I have the following code:

var i2 uint64;
var err error;
i2, err = uint64(strconv.ParseInt(scanner.Text(), 64, 64));

And I received the error:

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

According to everything I found on the Internet, this means that I am ignoring the two parameters returned by ParseInt, but I am using err. I know that maybe the mistake is very stupid, but I just started to learn Go and this confused me a lot.

icza
  • 389,944
  • 63
  • 907
  • 827
Nisanio
  • 4,056
  • 5
  • 34
  • 46

2 Answers2

9

The expression uint64(...) is a type conversion, and it cannot have multiple arguments (operands), but since strconv.ParseInt() has 2 return values, you're basically passing both to the type conversion, which is not valid.

Instead do this:

i, err := strconv.ParseInt(scanner.Text(), 64, 64)
// Check err
i2 := uint64(i)

Note that the base cannot be greater than 36, so you'll definitely get an error as you're passing 64 as the base.

Or use strconv.ParseUint() which will return you an uint value right away:

i, err := strconv.ParseUint(scanner.Text(), 16, 64)
// i is of type uint64, and ready to be used if err is nil

(Here I used a valid, 16 base. Use whatever you have to.)

Also see related question+answer: Go: multiple value in single-value context

icza
  • 389,944
  • 63
  • 907
  • 827
3

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

ParseInt returns 2 values: integer and error. So you cannot use them in function argument where only one value allowed. You may first get value and error then use value in next operations.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59