Introduction

The gcmd component supports reading user input data from the terminal, commonly used in terminal interaction scenarios.

Related methods:

  1. func Scan(info ...interface{}) string
  2. func Scanf(format string, info ...interface{}) string

Both methods display the given information to the terminal and automatically read user input from the terminal, returning it upon pressing the Enter key.

Scan Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gcmd"
  5. )
  6. func main() {
  7. name := gcmd.Scan("What's your name?\n")
  8. fmt.Println("Your name is:", name)
  9. }

After execution, interaction example:

  1. > What's your name?
  2. john
  3. > Your name is: john

Scanf Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gcmd"
  5. )
  6. func main() {
  7. name := gcmd.Scan("> What's your name?\n")
  8. age := gcmd.Scanf("> Hello %s, how old are you?\n", name)
  9. fmt.Printf("> %s's age is: %s", name, age)
  10. }

After execution, interaction example:

  1. > What's your name?
  2. john
  3. > Hello john, how old are you?
  4. 18
  5. > john's age is: 18