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
+18k Golang : Check if directory exist and create if does not exist
+4.8k Javascript : How to refresh page with JQuery ?
+7.7k Golang : Accept any number of function arguments with three dots(...)
+28.6k Golang : How to get HTTP request header information?
+11.4k Swift : Convert (cast) Int or int32 value to CGFloat
+4.7k Golang : Get S3 or CloudFront object or file information
+11.3k Golang : Search and extract certain XML data example
+5k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+39.8k Golang : How to check if a string contains another sub-string?
+18.1k Golang : Execute shell command
+11.2k Golang : calculate elapsed run time
+3.5k Javascript : How to show different content with noscript?