Reading an integer from standard input

yasith picture yasith · Sep 20, 2010 · Viewed 94.2k times · Source

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 the best way to read a single integer?

Answer

cthom06 picture cthom06 · Sep 20, 2010

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