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
+10.7k Golang : Replace a parameter's value inside a configuration file example
+14.1k How to automatically restart your crashed Golang server
+16.2k Golang : How to implement two-factor authentication?
+8.6k Golang : Find network service name from given port and protocol
+12.4k Golang : zlib compress file example
+5.9k Linux/Unix : Commands that you need to be careful about
+5.6k Golang : List all packages and search for certain package
+7.7k Golang : Handle Palindrome string with case sensitivity and unicode
+8.7k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+8.9k Golang : How to check if a string with spaces in between is numeric?
+7.5k Golang : Reverse a string with unicode
+11.8k Golang : Get remaining text such as id or filename after last segment in URL path