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
+32.6k Golang : Math pow(the power of x^y) example
+5.9k Golang : List all packages and search for certain package
+6.4k Golang : Selection sort example
+13.3k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+19.4k Golang : Check if directory exist and create if does not exist
+9.6k Golang : Apply Histogram Equalization to color images
+13.7k Golang : Read XML elements data with xml.CharData example
+5.5k Unix/Linux/MacOSx : How to remove an environment variable ?
+18.1k Golang : Defer function inside init()
+4.9k Fix Google Analytics Redundant Hostnames problem
+5.2k Swift : Convert (cast) Float to Int or Int32 value
+8.3k How to show different content from website server when AdBlock is detected?