Golang : Get input from keyboard
Writing text-based program to run from command line will not be completed without the ability to read input from the keyboard. Be it a single character or a string. Golang has three functions just to deal with this kind of situation. First is the fmt.Scanln() function, followed by fmt.Scan() function and fmt.Scanf() function.
Below is an example of fmt.Scanf() function
package main
import (
"fmt"
)
func main() {
var response int
fmt.Println("This program will activate SkyNet worldwide, are you sure about this?\n")
fmt.Scanf("%c", &response) //<--- here
switch response {
default:
fmt.Println("SkyNet launch aborted!")
case 'y':
fmt.Println("SkyNet launched")
case 'Y':
fmt.Println("SkyNet launched")
}
}
Hope this tutorial can help you out.
See also : Golang : Read input from console line
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
+12.4k Golang : How to get year, month and day?
+10.8k Golang : Save webcamera frames to video file
+5k Linux/MacOSX : Search for files by filename and extension with find command
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+9.1k Golang : Get login name from environment and prompt for password
+9.4k Golang : Flip coin example
+45.5k Golang : Convert int to byte array([]byte)
+9k Golang : Get escape characters \u form from unicode characters
+4.9k Golang : Find change in a combination of coins example
+10k Golang : Characters limiter example
+4.6k How to check with curl if my website or the asset is gzipped ?
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error