Golang : How to read integer value from standard input ?
Reading integer value from standard input in Golang is fairly simple with fmt.Scanf function. The code below demonstrate how to capture integer value from the standard input.
package main
import (
"fmt"
)
func main() {
var i int
fmt.Println("Enter an integer value : ")
_, err := fmt.Scanf("%d", &i)
if err != nil {
fmt.Println(err)
}
fmt.Println("You have entered : ", i)
}
See also : Golang : How to read float value from standard input ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.2k Golang : Delete files by extension
+6.2k Golang : Spell checking with ispell example
+18.5k Golang : Delete duplicate items from a slice/array
+6k Golang : Test input string for unicode example
+13.2k Golang : Get user input until a command or receive a word to stop
+7.5k Golang : Lock executable to a specific machine with unique hash of the machine
+13.7k Javascript : Prompt confirmation before exit
+54.7k Golang : Unmarshal JSON from http response
+5.7k AWS S3 : Prevent Hotlinking policy
+10.8k Golang : Fix - does not implement sort.Interface (missing Len method)
+23.1k Find and replace a character in a string in Go
+9.2k Golang : Read file with ioutil