Getting Integer Input from Standard Input
Reading integer values from standard input is a common requirement in programming. In Go, the fmt.Scanf function provides a convenient way to retrieve numeric input.
Using fmt.Scanf
To use fmt.Scanf, first declare a variable of the desired type (e.g., var i int). Then, call fmt.Scanf with the following syntax:
fmt.Scanf("%d", &i)
Here, "%d" specifies the format specifier for integers. The & character before i is the memory address operator, which allows fmt.Scanf to modify the value of i.
Alternative Methods
If fmt.Scanf is not suitable, other options include:
Example Usage
The following code demonstrates how to read an integer using fmt.Scanf:
package main import ( "fmt" ) func main() { var input int fmt.Scanf("%d", &input) fmt.Println("Input:", input) }
The above is the detailed content of How Can I Read Integer Input from Standard Input in Go?. For more information, please follow other related articles on the PHP Chinese website!