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
+20.6k Golang : Convert date string to variants of time.Time type examples
+14.3k Golang : How to check if your program is running in a terminal
+24.3k Golang : GORM read from database example
+12.7k Golang : Convert int(year) to time.Time type
+8.8k Golang : Sort lines of text example
+18.9k Golang : Check whether a network interface is up on your machine
+14.8k Golang : Basic authentication with .htpasswd file
+7k Golang : Transform lisp or spinal case to Pascal case example
+11.2k Golang : How to flush a channel before the end of program?
+15.4k Golang : Validate hostname
+51.7k Golang : How to get time in milliseconds?
+20.5k Golang : Read directory content with os.Open