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 : Calculate percentage change of two values
+5.7k Golang : Function as an argument type example
+26.5k Golang : Convert file content into array of bytes
+5.6k Unix/Linux : How to test user agents blocked successfully ?
+30.2k Golang : How to redirect to new page with net/http?
+5.4k Golang : Shortening import identifier
+40.8k Golang : How to count duplicate items in slice/array?
+11.9k Golang : Pagination with go-paginator configuration example
+17.2k Golang : Multi threading or run two processes or more example
+10.8k How to test Facebook App on localhost ?
+4.7k Javascript : How to get width and height of a div?
+5.2k Golang : fmt.Println prints out empty data from struct