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
+15.1k Golang : Test floating point numbers not-a-number and infinite example
+8.9k Golang : Setting variable value with ldflags
+17.7k Golang : Delete duplicate items from a slice/array
+18.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+8.2k Golang : Extract or copy items from map based on value
+6k Golang : Fibonacci number generator examples
+7.3k Swift : Convert (cast) Character to Integer?
+12k Golang : Get user input until a command or receive a word to stop
+30.1k Golang : Math pow(the power of x^y) example
+11.1k Golang : Forwarding a local port to a remote server example
+16.2k Golang : How to remove certain lines from a file
+25.3k Golang : Force your program to run with root permissions