Golang : Read input from console line
Ok, this is another tutorial on how to read input from console in Golang. It uses the bufio.NewReader() function and accept input from standard input via os.Stdin. Simple and straight forward example below:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
consolereader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your name : ")
input, err := consolereader.ReadString('\n') // this will prompt the user for input
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(input)
}
Output :
Enter your name : BooBooBoy
BooBooBoy
https://www.socketloop.com/tutorials/golang-get-input-from-keyboard
See also : Golang : Get input from keyboard
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
+7.6k Golang : How to execute code at certain day, hour and minute?
+5k Python : Create Whois client or function example
+10.3k Android Studio : Simple input textbox and intercept key example
+11.4k Golang : Display a text file line by line with line number example
+25.5k Golang : How to write CSV data to file
+12.1k Golang : Print UTF-8 fonts on image example
+30.7k error: trying to remove "yum", which is protected
+9.1k Golang : Generate random Chinese, Japanese, Korean and other runes
+19.9k Golang : Reset or rewind io.Reader or io.Writer
+19.6k Golang : Count JSON objects and convert to slice/array
+37.8k Golang : Read a text file and replace certain words
+16.3k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error