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
+17.1k Golang : Find file size(disk usage) with filepath.Walk
+31.8k Golang : Convert an image file to []byte
+11.9k Golang : Convert a rune to unicode style string \u
+4.7k Which content-type(MIME type) to use for JSON data
+12.4k Golang : HTTP response JSON encoded data
+13.4k Android Studio : Password input and reveal password example
+8.2k Useful methods to access blocked websites
+12.5k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+11k Golang : Simple image viewer with Go-GTK
+11.5k CodeIgniter : Import Linkedin data
+19.7k Golang : Append content to a file
+15.4k Golang : rune literal not terminated error