Converting string to bigInt

Hi there,

I am trying to convert the string to big.Int using the big.SetString method for the following code, however, I got error.

package main

import (
	"fmt"
	"math/big"
	"strings"
)

func main() {
	var val = "100su"
	if val[len(val)-2:] == "su" {
		val = strings.TrimSuffix(val, "su")
		fmt.Println(val)


		if _, failed := new(big.Int).SetString(val, 10); failed {
			fmt.Println("error")
			return
		}

		fmt.Println("succeed")
		return
	}

}

The following is the output:

100
error

As epxected, the provided string is just a number, however, when trying to convert the number, it failed.

Any help if appreciated.

Best,
Shawn

if _, failed := new(big.Int).SetString(val, 10); failed {

should be

if _, ok := new(big.Int).SetString(val, 10); !ok {

the boolean returned is true on success

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.