Skip to main content
Version: 2.7.x

基本介绍

gcmd 组件支持从终端读取用户输入数据,常用于终端交互场景。

相关方法:

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

这两个方法都是向终端展示给定的信息,自动读取终端用户输入的信息返回,通过回车符号返回。

Scan 使用

package main

import (
"fmt"

"github.com/gogf/gf/v2/os/gcmd"
)

func main() {
name := gcmd.Scan("What's your name?\n")
fmt.Println("Your name is:", name)
}

执行后,交互示例:

> What's your name?
john
> Your name is: john

Scanf 使用

package main

import (
"fmt"

"github.com/gogf/gf/v2/os/gcmd"
)

func main() {
name := gcmd.Scan("> What's your name?\n")
age := gcmd.Scanf("> Hello %s, how old are you?\n", name)
fmt.Printf("> %s's age is: %s", name, age)
}

执行后,交互示例:

> What's your name?
john
> Hello john, how old are you?
18
> john's age is: 18