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
+22.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+5.9k Golang : How to call function inside template with template.FuncMap
+4.6k Unix/Linux : How to find out the hard disk size?
+32.6k Golang : Proper way to set function argument default value
+6.9k Android Studio : Rating bar example
+3.9k Javascript : How to get width and height of a div?
+8.3k Golang : Find correlation coefficient example
+10.8k Golang : Simple client-server HMAC authentication without SSL example
+8.4k Golang : Convert(cast) string to int64
+4k Golang : Print instead of building pyramids
+9.9k Golang : Read until certain character to break for loop