fmt.Scan can read one space-separated value from the terminal.
Go code
var name string
fmt.Print("Name: ")
fmt.Scan(&name)
fmt.Println("Hello", name)Line-by-line explanation
- A string variable is declared.
- Print displays a prompt without a new line.
- Scan reads into name; the ampersand supplies its address.
- Println displays the greeting.
Expected output
Name: Maya
Hello MayaThe user typed Maya after the prompt. Pointers are explained slowly in Stage 7.
For full lines containing spaces, a bufio.Scanner is more suitable and will be introduced when file and input handling are deeper.