In the example, we will show you how to read input from a user with Scanf in the Go language.
Go read input with Scanf
The Scanf function scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.
Let's create a file named go_example.go and add the following content to it:
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scanf("%s", &name)
fmt.Println("Hello", name)
}
Output:
G:\GoLang\examples>go run go_example.go Enter your name: John Hello John
The example prompts the user to enter his name.