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
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+19.6k Golang : How to get own program name during runtime ?
+7.9k Golang : Reverse text lines or flip line order example
+26k Golang : Calculate future date with time.Add() function
+11.1k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+6.6k Get Facebook friends working in same company
+9.2k Facebook : Getting the friends list with PHP return JSON format
+8.1k Your page has meta tags in the body instead of the head
+7.9k Golang : HttpRouter multiplexer routing example
+21.6k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+5.7k nginx : force all pages to be SSL
+9.9k Golang : Random Rune generator