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
+26.7k Golang : Change a file last modified date and time
+18.9k nginx: [emerg] unknown directive "passenger_enabled"
+21k Golang : Read directory content with filepath.Walk()
+3.9k Which content-type(MIME type) to use for JSON data
+16.7k Golang : Get all upper case or lower case characters from string example
+7.6k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+6.2k Golang : Example of custom handler for Gorilla's Path usage.
+6.9k Golang : get the current working directory of a running program
+17.4k Golang : Padding data for encryption and un-padding data for decryption
+21.3k Golang : Round float to precision example
+12.1k Golang : Calculate elapsed years or months since a date