多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] Survey 用于在支持 `ANSI` 转义序列的终端上构建交互式且可访问的提示。 使用 `Golang` 编写命令时,有交互部分使用这个库会觉得上档次且能够得到很友好的提示。交互部分提供有好几种方式。 交互模式有 `Input` 、 `Multiline` 、 `Password` 、 `Confirm` 、 `Select` 、 `MultiSelect` 和 `Editor` 。 下面演示每种模式示例 ## Input 交互输入的内容保存到变量中,但不能保存多个多行内容。因为回车就标识交互输入结束 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { var name string prompt := &survey.Input{ Message: "Enter your name: ", } if err := survey.AskOne(prompt, &name); err != nil { fmt.Printf("err: %v\n", err) } else { fmt.Printf("Your name: %v\n", name) } } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Enter your name: jiaxzeng # 运行结束 $ go run main.go ? Enter your name: jiaxzeng Your name: jiaxzeng ``` ## Multiline 交互输入的内容保存到变量中,可以识别换行符。结束输入时,需要练习输入两个换行换。 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { var message string prompt := &survey.Multiline{ Message: "Enter your message: ", } if err := survey.AskOne(prompt, &message); err != nil { fmt.Printf("err: %v\n", err) } else { fmt.Printf("You input message: %v\n", message) } } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Enter your message: [Enter 2 empty lines to finish] jiaxzeng xiaodunan # 运行结束 $ go run main.go ? Enter your message: [Enter 2 empty lines to finish] jiaxzeng xiaodunan ? Enter your message: jiaxzeng xiaodunan You input message: jiaxzeng xiaodunan ``` ## Password 交互输入的内容保存到变量中,将输入的字符串用 *** 来代替。 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Enter your password: ******** # 运行结束 $ go run main.go ? Enter your password: ******** Password entered: jiaxzeng ``` ## Confirm 交互输入的内容保存到变量中,询问是否确认该操作。 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { var ok bool prompt := &survey.Confirm{ Message: "Enter Your Confirm", } if err := survey.AskOne(prompt, &ok); err != nil { fmt.Printf("err: %v\n", err) } else { fmt.Printf("Your confirm is: %v\n", ok) } } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Enter Your Confirm (y/N) n # 运行结束 $ go run main.go ? Enter Your Confirm No Your confirm is: false ``` ## Select 多个选项提供给你选择,将选择的结果保存在变量中 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { var color string prompt := &survey.Select{ Message: "Choose a color:", Options: []string{"red", "blue", "green"}, } if err := survey.AskOne(prompt, &color); err != nil { fmt.Printf("err: %v\n", err) } else { fmt.Printf("Your choose color is %v\n", color) } } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Choose a color: [Use arrows to move, type to filter] > red blue green # 运行结束 $ go run main.go ? Choose a color: red Your choose color is red ``` ## MultiSelect 可以多选赋值给变量,选择使用空格键,右键选择所有选项,左键取消所有选项,回车确认选择完成。 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { days := []string{} prompt := &survey.MultiSelect{ Message: "What days do you prefer:", Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, } if err := survey.AskOne(prompt, &days); err != nil { fmt.Printf("err: %v\n", err) } else { fmt.Printf("Your choose day: %v\n", days) } } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? What days do you prefer: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter] [x] Sunday > [x] Monday [ ] Tuesday [ ] Wednesday [ ] Thursday [x] Friday [x] Saturday # 运行结束 $ go run main.go ? What days do you prefer: Sunday, Monday, Friday, Saturday Your choose day: [Sunday Monday Friday Saturday] ``` ## Editor 生成代码片。如果编辑器有高亮语法的话,填写的时候会有高亮提醒。会打开一个文件给用户写代码片,保存后会删除该文件。把写入文件的内容赋值给变量。 代码示例 ```go package main import ( "fmt" survey "github.com/AlecAivazis/survey/v2" ) func main() { } ``` 运行结果 ```bash # 运行过程 $ go run main.go ? Shell code snippet [Enter to launch editor] # 回车 打开一个文件给用户编写代码片,写完记得保存退出 # 运行结束 $ go run main.go ? Shell code snippet <Received> content: jiaxzeng ``` ## 文档 Golang pkg:https://pkg.go.dev/github.com/AlecAivazis/survey/v2 GitHub:https://github.com/go-survey/survey