Reading an integer from standard input

前端 未结 5 1952
轻奢々
轻奢々 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

    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)
    }
    

提交回复
热议问题