Go语言学习: survey 包 / ipt / 交互 list/select

一个可以产生交互式 ui 的 golang 包
更新于: 2024-11-16 20:52:40

安装

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

main.go

package main

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

func BuildCommandList(values []string) {
	// the answers will be written to this struct
	answers := struct {
		Module string `survey:"module"`
	}{}

	// the questions to ask
	var qs = []*survey.Question{
		{
			Name: "module",
			Prompt: &survey.Select{
				Message: "Choose a module:",
				Options: values,
			},
		},
	}

	// perform the questions
	err := survey.Ask(qs, &answers)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println("submodule: ", answers.Module);
}


func main() {
  values := []string{"auth", "blog", "core", "user"}
  BuildCommandList(values)
}