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
+5.9k Golang : Generate multiplication table from an integer example
+30.1k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+12.3k Golang : List running EC2 instances and descriptions
+11.5k CodeIgniter : Import Linkedin data
+6.9k Fix sudo yum hang problem with no output or error messages
+23k Golang : Test file read write permission example
+5.2k Golang : The Tao of importing package
+6.3k Unix/Linux : Use netstat to find out IP addresses served by your website server
+19.9k Golang : Count JSON objects and convert to slice/array
+7.3k Golang : Not able to grep log.Println() output
+19.8k Golang : Append content to a file
+16k Golang : How to reverse elements order in map ?