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
+30.7k Golang : Remove characters from string example
+10.3k Golang : Use regular expression to get all upper case or lower case characters example
+14.6k Golang : Overwrite previous output with count down timer
+11.4k Golang : Intercept and process UNIX signals example
+7.1k Golang : Validate credit card example
+36.1k Golang : Integer is between a range
+19.9k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+14.3k Elastic Search : Mapping date format and sort by date
+12.2k Golang : Detect user location with HTML5 geo-location
+5.1k Google : Block or disable caching of your website content
+8.1k Golang : Get all countries phone codes
+25.7k Golang : convert rune to integer value