Reading an integer from standard input

前端 未结 5 1941
轻奢々
轻奢々 2020-12-12 18:58

How do I use the fmt.Scanf function in Go to get an integer input from the standard input?

If this can\'t be done using fmt.Scanf, what\'s

相关标签:
5条回答
  • 2020-12-12 19:10

    Here is my "Fast IO" method for reading positive integers. It could be improved with bitshifts and laying out memory in advance.

    package main
    
    import (
        "io/ioutil"
        "bufio"
        "os"
        "strconv"
    )
    
    
    func main() {
        out := bufio.NewWriter(os.Stdout)
        ints := getInts()
        var T int64
        T, ints = ints[0], ints[1:]
        ..
        out.WriteString(strconv.Itoa(my_num) + "\n")
        out.Flush()
        }
    }
    
    func getInts() []int64 {
        //assumes POSITIVE INTEGERS. Check v for '-' if you have negative.
        var buf []byte
        buf, _ = ioutil.ReadAll(os.Stdin)
        var ints []int64
        num := int64(0)
        found := false
        for _, v := range buf {
            if '0' <= v && v <= '9' {
                num = 10*num + int64(v - '0') //could use bitshifting here.
                found = true
            } else if found {
                ints = append(ints, num)
                found = false
                num = 0
            }
        }
        if found {
            ints = append(ints, num)
            found = false
            num = 0
        }
        return ints
    }
    
    0 讨论(0)
  • 2020-12-12 19:10

    You can use fmt.Scanf with a format specifier. The format specifier for the integer is %d. So you can use standard input like below.

    func main() {
        var someVar int
        fmt.Scanf("%d", &someVar)
    }
    

    or else you can use fmt.Scan or fmt.Scanln as below.

    func main() {
       var someVar int
       fmt.Scanln(&someVar)
    }
    
    0 讨论(0)
  • 2020-12-12 19:34

    An alternative that can be a bit more concise is to just use fmt.Scan:

    package main
    
    import "fmt"
    
    func main() {
        var i int
        fmt.Scan(&i)
        fmt.Println("read number", i, "from stdin")
    }
    

    This uses reflection on the type of the argument to discover how the input should be parsed.

    http://golang.org/pkg/fmt/#Scan

    0 讨论(0)
  • 2020-12-12 19:34

    Golang fmt.Scan is simpler than Golang fmt.Scanf (which is simpler than Clang scanf)

    If fmt.Scan errors i.e. if not nil, log & return

    1 Read single variable:

    import (
        "fmt"
        "log"
    )
    
    var i int
    if    _, err := fmt.Scan(&i);    err != nil {
        log.Print("  Scan for i failed, due to ", err)
        return
    }
    
    fmt.Println(i)
    

    2 Read multiple variables:

    import (
        "fmt"
        "log"
    )
    
    var i, j, k int  
    if    _, err := fmt.Scan(&i, &j, &k);    err != nil {
        log.Print("  Scan for i, j & k failed, due to ", err)
        return
    }
    
    fmt.Println(i, j, k)
    

    Best of luck

    Example from: http://www.sortedinf.com/?q=golang-in-1-hour

    0 讨论(0)
  • 2020-12-12 19:35

    http://golang.org/pkg/fmt/#Scanf

    All the included libraries in Go are well documented.

    That being said, I believe

    func main() {
        var i int
        _, err := fmt.Scanf("%d", &i)
    }
    

    does the trick

    0 讨论(0)
提交回复
热议问题