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
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+7k Golang : How to fix html/template : "somefile" is undefined error?
+6.5k Golang : Humanize and Titleize functions
+4.7k Unix/Linux : secure copying between servers with SCP command examples
+21.3k SSL : How to check if current certificate is sha1 or sha2
+9.9k Golang : Print how to use flag for your application example
+9k Golang : Apply Histogram Equalization to color images
+9.6k Golang : Get current, epoch time and display by year, month and day
+6.8k Web : How to see your website from different countries?
+4.7k Nginx and PageSpeed build from source CentOS example
+20.5k Golang : Underscore or snake_case to camel case example
+5.1k Golang : Get FX sentiment from website example