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
+14.9k Golang : Normalize unicode strings for comparison purpose
+10.7k Golang : How to delete element(data) from map ?
+19.3k Mac OSX : Homebrew and Golang
+7.2k Nginx : How to block user agent ?
+16.1k Golang : Generate universally unique identifier(UUID) example
+9.6k Golang : Accessing content anonymously with Tor
+20.2k Golang : How to run your code only once with sync.Once object
+6.4k Golang : Extract sub-strings
+30.9k error: trying to remove "yum", which is protected
+5.4k Python : Convert(cast) string to bytes example
+6.3k Golang : Get missing location after unmarshal binary and gob decode time.