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
+15.8k Golang : XML to JSON example
+29.5k Golang : bufio.NewReader.ReadLine to read file line by line
+8.8k Golang : Detect number of faces or vehicles in a photo
+19.9k Golang : How to get time zone and load different time zone?
+12.8k Golang : convert(cast) string to float value
+21.4k Golang : simulate tail -f or read last line from log file example
+12.5k Golang : Check if an integer is negative or positive
+13.7k Golang : How to check if IP address is in range
+11.4k Golang : Exit, terminating or aborting a program
+30.5k Golang : Copy directory - including sub-directories and files
+6.7k Golang : Reverse a string with unicode