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
+14.4k Golang : How to reverse elements order in map ?
+17.2k Golang : Check whether a network interface is up on your machine
+12.2k Golang : Set image canvas or background to transparent
+6.7k Golang : Error reading timestamp with GORM or SQL driver
+18.4k Golang : Append content to a file
+10.3k Golang : Byte format example
+5.3k Grep : How to grep for strings inside binary data
+18k Golang : Convert seconds to human readable time format example
+7.1k Golang : Routes multiplexer routing example with regular expression control
+11.9k Golang : Calculate elapsed years or months since a date
+10.9k Golang : Get month name from date example
+12.1k Golang : Get dimension(width and height) of image file