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.9k Golang : How to shuffle elements in array or slice?
+9.2k Golang : Convert IP version 6 address to integer or decimal number
+9.6k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+11.3k Golang : Aligning strings to right, left and center with fill example
+9.9k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+4.7k Mac OSX : Find large files by size
+2.8k Golang : How to solve "too many .rsrc sections" error?
+10.4k Golang : GUI with Qt and OpenCV to capture image from camera
+7.1k Golang : Meaning of omitempty in struct's field tag
+41.1k Golang : Unmarshal JSON from http response
+5.7k Golang : Get SPF and DMARC from email headers to fight spam
+16.9k Mac OSX : Homebrew and Golang