Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an integer from standard input

Tags:

stdin

go

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?

like image 297
yasith Avatar asked Sep 20 '10 12:09

yasith


People also ask

How do you read an integer in standard input?

To read an integer with scanf, use %d . Mixing scanf and get / fgets doesn't work well. Anyway scanf is not at all made for interactive user input. See answer below.

How to read integer value from the standard input in java?

To read integers from console, use Scanner class. Scanner myInput = new Scanner( System.in ); Allow a use to add an integer using the nextInt() method. System.

How to read standard input C?

char c; scanf(" %c", &c); reads the next non-white-space character in the standard input and stores it into variable c. is a typical way to use scanf. Expression eof() is true if an attempt to read the standard input has been done, and the standard input was found to have nothing left in it.

How do you read integers?

Let's take a look at how we read and write integers. Numbers like eight, negative four, 15, negative 76, and zero are all examples of integers. An integer is any number in this set, where the dot dot dots on either side means continues without end. The word “integer” comes from the Latin word that means “whole.”


1 Answers

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

like image 145
cthom06 Avatar answered Sep 21 '22 13:09

cthom06