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
+20.3k Golang : Reset or rewind io.Reader or io.Writer
+10.3k Golang : Bcrypting password
+25.4k Golang : Get current file path of a file or executable
+23.2k Golang : Randomly pick an item from a slice/array example
+5.6k PHP : Fix Call to undefined function curl_init() error
+19.4k Golang : Populate dropdown with html/template example
+5.7k Javascript : How to refresh page with JQuery ?
+24.1k Golang : Use regular expression to validate domain name
+46.3k Golang : Read tab delimited file with encoding/csv package
+10k Golang : Translate language with language package example
+4.8k Golang : How to pass data between controllers with JSON Web Token
+9.7k Golang : Sort and reverse sort a slice of floats