Go read input from user with NewReader

In the example, we will show you how to read input from a user with NewReader in the Go language.

Go read input from user with NewReader

The program reads a name from the input using bufio.NewReader.

Let's create a file named go_example.go and add the following content to it:


package main

import (
     "os"
     "bufio"
     "fmt"
)

func main() {

     reader := bufio.NewReader(os.Stdin)

     fmt.Print("Enter your name: ")

     name, _ := reader.ReadString('\n')
     fmt.Printf("Hello %s\n", name)
}

Output:

G:\GoLang\examples>go run go_example.go
Enter your name: John
Hello John

Comments