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
+10.8k Golang : Replace a parameter's value inside a configuration file example
+36.3k Golang : Validate IP address
+8k Golang : Tell color name with OpenCV example
+7.7k Golang : Example of how to detect which type of script a word belongs to
+4.9k Linux : How to set root password in Linux Mint
+14.4k Golang : Rename directory
+29.3k Golang : Login(Authenticate) with Facebook example
+10.4k Golang : Select region of interest with mouse click and crop from image
+18.4k Golang : Set, Get and List environment variables
+6.4k Grep : How to grep for strings inside binary data
+12.1k Golang : Print UTF-8 fonts on image example
+8.9k Golang : How to use Gorilla webtoolkit context package properly