Scan input strings from STDIN


We'll have a lot more to say about getting input from the user, but in order to be able to write some useful code, let's introduce a simple way to get input from the user. For this we need to declare a variable first to be a string. Then we call the Scan function and give the variable as a parameter. More precisely we pass a pointer to the variable to the function by prefixing it with &. Later we'll discuss pointers as well, for now all you need to know is that Scan will wait till the user types in something and presses ENTER. Then all the content will be in the variable.

examples/scan/scan.go
package main

import (
    "fmt"
)

func main() {
    var name string // declare variable as a string without assignment
    fmt.Print("Your name: ")
    fmt.Scan(&name)
    fmt.Printf("Hello %s, how are you?\n", name)
    fmt.Printf("Type %T\n", name) // string
}