How to read data from standard input (stdin) in a Go console application?
Hello everyone! I just started learning Go and I'm writing a simple console utility. I need the program to ask the user for name and age, and then output a greeting. But I can't figure out how to properly read the string that the user enters in the terminal.
I tried using `fmt.Scan()`, but it seems to only read up to the first space. If I enter "Ivan Petrov", it only reads "Ivan". What if I need to read an entire string with spaces? Or a number?
Here's my current code that doesn't work as expected:
```go
package main
import "fmt"
func main() {
var name string
var age int
fmt.Print("Enter name: ")
fmt.Scan(&name) // Problem: reads only up to space
fmt.Print("Enter age: ")
fmt.Scan(&age)
fmt.P
No answers yet.
Log in to leave an answer
Blogs