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
+4.3k Gogland : Datasource explorer
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+7.8k Yum Error: no such table: packages
+5.6k Golang : When to use make or new?
+9.3k Golang : Meaning of omitempty in struct's field tag
+19.9k Golang : GORM create record or insert new record into database example
+29.6k error: trying to remove "yum", which is protected
+5.5k Golang : Embedded or data bundling example
+17.6k Golang : Get host name or domain name from IP address
+7.1k Golang : Reverse text lines or flip line order example
+11.1k Golang : Forwarding a local port to a remote server example