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
+21.6k Golang : How to run Golang application such as web server in the background or as daemon?
+17.5k Golang : Get all upper case or lower case characters from string example
+8.6k Golang : Inject/embed Javascript before sending out to browser example
+9.1k Golang : Extract or copy items from map based on value
+11.8k Golang : List running EC2 instances and descriptions
+7.2k Golang : Rename part of filename
+5.2k Unix/Linux : How to find out the hard disk size?
+13.1k Golang : Strings comparison
+21.7k Golang : Securing password with salt
+10.9k Android Studio : Create custom icons for your application example
+4.5k Golang : A program that contain another program and executes it during run-time