go-ipt: 一个方便的列表选择功能

类似于 nodejs 里的ipt包
更新于: 2024-11-28 12:41:37
项目主页: https://github.com/afeiship/go-ipt

安装

go get -u github.com/afeiship/go-ipt

简单

字符串列表

package main

import (
	"fmt"

	"github.com/afeiship/go-ipt"
)

func main() {
	color, err := ipt.IptRaw("What is your favorite color?", []string{"Red", "Green", "Blue"})
	if err != nil {
		panic(err)
	}
	fmt.Println(color)
}

复杂对象

label + any 的组合

package main

import (
	"github.com/afeiship/go-ipt"
)

func main() {
	type ColorObj struct {
		Name string
		Hex  string
	}

	opts := []ipt.Option[ColorObj]{
		{Label: "Red", Value: ColorObj{"Red", "#FF0000"}},
		{Label: "Green", Value: ColorObj{"Green", "#00FF00"}},
		{Label: "Blue", Value: ColorObj{"Blue", "#0000FF"}},
	}

	color, err := ipt.Ipt("What is your favorite color?", opts)
	if err != nil {
		panic(err)
	}
	println(color.Name, color.Hex)
}