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
+12.7k Elastic Search : Mapping date format and sort by date
+5k Golang : Extract XML attribute data with attr field tag example
+19.7k Golang : Encrypt and decrypt data with TripleDES
+10.4k Golang : Get remaining text such as id or filename after last segment in URL path
+7.9k Golang : Go as a script or running go with shebang/hashbang style
+10.9k Android Studio : Highlight ImageButton when pressed on example
+12.7k Golang : Get current time
+6k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+5.4k Golang : Find the longest line of text example
+40.1k Golang : Convert string to array/slice
+27.4k Golang : Saving(serializing) and reading file with GOB
+10.8k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard