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
+8.9k Golang : How to get garbage collection data?
+7.7k Golang : Get final or effective URL with Request.URL example
+6.9k Linux : How to fix Brother HL-1110 printing blank page problem
+38.6k Golang : How to read CSV file
+7k Golang : Individual and total number of words counter example
+20.3k Golang : Convert PNG transparent background image to JPG or JPEG image
+11.2k Golang : Display a text file line by line with line number example
+28.9k Golang : JQuery AJAX post data to server and send data back to client example
+4.5k JQuery : Calling a function inside Jquery(document) block
+5.4k Golang : Struct field tags and what is their purpose?
+21.7k Golang : Print leading(padding) zero or spaces in fmt.Printf?