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
+17.4k Golang : Linked list example
+19.2k Golang : Get RGBA values of each image pixel
+8.7k Golang : On lambda, anonymous, inline functions and function literals
+17.9k Golang : Get all upper case or lower case characters from string example
+10.1k Golang : Compare files modify date example
+27.8k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+10.4k Swift : Convert (cast) String to Integer
+44.7k Golang : Use wildcard patterns with filepath.Glob() example
+18.8k Golang : Read input from console line
+19.2k Golang : Get host name or domain name from IP address
+8.7k Android Studio : Image button and button example
+46.3k Golang : Encode image to base64 example