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.7k Golang : package is not in GOROOT during compilation
+47.2k Golang : Convert int to byte array([]byte)
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+20k nginx: [emerg] unknown directive "passenger_enabled"
+6.6k Golang : Calculate pivot points for a cross
+21.8k Golang : Convert seconds to minutes and remainder seconds
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+8.9k Golang : Gonum standard normal random numbers example
+12.4k Golang : Transform comma separated string to slice example
+19k Golang : Delete item from slice based on index/key position
+6.4k Golang : Skip or discard items of non-interest when iterating example
+10.9k Golang : Roll the dice example