Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan a big.Int from standard input in Go

Tags:

go

biginteger

Is there a way to scan a big.Int directly from the standard input in Go? Right now I'm doing this:

package main

import (
    "fmt"
    "math/big"
)

func main() {
    w := new(big.Int)
    var s string
    fmt.Scan(&s)
    fmt.Sscan(s, w)
    fmt.Println(w)
}

I also could have used .SetString. But, is there a way to Scan the big.Int directly from the standard input without scanning a string or an integer first?

like image 285
pjox Avatar asked Jan 23 '26 12:01

pjox


1 Answers

For example,

package main

import (
    "fmt"
    "math/big"
)

func main() {
    w := new(big.Int)
    n, err := fmt.Scan(w)
    fmt.Println(n, err)
    fmt.Println(w.String())
}

Input (stdin):

295147905179352825857

Output (stdout):

1 <nil>
295147905179352825857
like image 185
peterSO Avatar answered Jan 25 '26 15:01

peterSO



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!