Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 560
Mangano Avatar asked Apr 21 '26 23:04

Mangano


1 Answers

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

like image 55
icza Avatar answered Apr 24 '26 16:04

icza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!