golang命令行交互

  • A+
所属分类:golang

安装依赖

go get -u github.com/AlecAivazis/survey/v2

示例代码

package main

import (
	"fmt"
	"github.com/AlecAivazis/survey/v2"
	"log"
)

var qs = []*survey.Question{
	{
		Name: "name",
		Prompt: &survey.Input{Message: "What Is Your Name?"},
		Validate: survey.Required,
		Transform: survey.Title,
	}, {
		Name: "color",
		Prompt: &survey.Select{
			Message: "Choose a color:",
			Options: []string{"red", "blue", "green"},
			Default: "blue",
		},
	},
}

func main()  {
	answers := struct {
		Name string
		FavoriteColor string `survey:"color"`
	}{}

	err := survey.Ask(qs, &answers)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s chose %s", answers.Name, answers.FavoriteColor)
}

执行结果

golang命令行交互

weinxin
我的微信
欢迎来撩!!
admin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: