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
+10.6k Golang : convert(cast) float to string
+9.1k Golang : Convert file content to Hex
+4.5k Golang : Shortening import identifier
+5.8k How to let Facebook Login button redirect to a particular URL ?
+25.2k Golang : Find files by extension
+19.7k Curl usage examples with Golang
+24.8k Golang : Convert(cast) string to uint8 type and back to string
+11.3k Golang : Arithmetic operation with numerical slices or arrays example
+7.6k Golang : Executing and evaluating nested loop in html template
+11.9k Golang : List objects in AWS S3 bucket