Reading standard input
Every process owns its standard input, output, and error file descriptor. The stdin serves as the input of the process. This recipe describes how to read the data from the stdin.
How to do it...
- Open the console and create the folder
chapter05/recipe01. - Navigate to the directory.
- Create the
fmt.gofile with the following content:
package main
import (
"fmt"
)
func main() {
var name string
fmt.Println("What is your name?")
fmt.Scanf("%s\n", &name)
var age int
fmt.Println("What is your age?")
fmt.Scanf("%d\n", &age)
fmt.Printf("Hello %s, your age is %d\n", name, age)
}- Execute the code with
go run fmt.go. - Enter the input
Johnand press Enter. - Enter the input
40and press Enter.
- You will see the following output:

- Create the file
scanner.gowith the following content:
package main
import (
"bufio"
"fmt"
...