基本介绍
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("Whats your name?
")
fmt.Println("Your name is:", name)
}
执行后,交互示例:
> Whats your name?
john
> Your name is: john
Scanf使用
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gcmd"
)
func main() {
name := gcmd.Scan("> Whats your name?
")
age := gcmd.Scanf("> Hello %s, how old are you?
", name)
fmt.Printf("> %ss age is: %s", name, age)
}
执行后,交互示例:
> Whats your name?
john
> Hello john, how old are you?
18
> johns age is: 18
作者:冒牌SEO,如若转载,请注明出处:https://www.web176.com/goframe/21117.html