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.5k Golang : Get executable name behind process ID example
+14.8k Golang : Get URI segments by number and assign as variable example
+9.7k Golang : Populate slice with sequential integers example
+5.3k Golang : Get FX sentiment from website example
+11.6k Get form post value in Go
+5.6k PHP : Convert CSV to JSON with YQL example
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+29.9k Golang : Get and Set User-Agent examples
+19.4k Golang : Get host name or domain name from IP address
+18k Golang : Qt image viewer example
+7.2k Golang : Null and nil value