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
+8.7k Golang : Read file and convert content to string
+8.5k Golang : cannot assign type int to value (type uint8) in range error
+6.4k Golang : get the current working directory of a running program
+13k nginx: [emerg] unknown directive "ssl"
+3.4k Golang : How to pass data between controllers with JSON Web Token
+7.8k Golang : ffmpeg with os/exec.Command() returns non-zero status
+20k Golang : How to reverse slice or array elements order
+22.7k Golang : Change file read or write permission example
+10.5k Golang : Listen and Serve on sub domain example
+10k Golang : Perform sanity checks on filename example
+8.5k Golang : Convert file content to Hex
+4.6k Golang : Compound interest over time example