普通网友 2014-03-15 06:55
浏览 55
已采纳

如何从Go中的标准输入扫描big.Int

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?

  • 写回答

2条回答 默认 最新

  • dongshanjin8947 2014-03-15 08:23
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?