如何使用 Golang 询问 "Yes" 或 "No"

How to ask "Yes" or "No" using Golang

我正在使用 promptUi 创建一个 select 列表。现在我想在 selection 之后提示 "Yes" 或 "No" 问题:

bold := color.New(color.Bold).SprintFunc()
cellTemplate := &promptui.SelectTemplates{
    Label:    "{{ . }}",
    Active:   "\U000027A4 {{ .| bold }}",
    Inactive: "  {{ . | faint }}",
    Help:     util.Faint("[Use arrow keys]"),
}

cellPrompt := promptui.Select{
    Label:     util.YellowBold("?") + " Select an environment to be installed",
    Items:     getCreateEnvironmentList(),
    Templates: cellTemplate,
}
_, value, err := cellPrompt.Run()
if err != nil {
    return fmt.Errorf("Failed to select: %v", err)
}

switch value {
case constants.CELLERY_CREATE_LOCAL:
    {
        // Prompt yes or no
    }
case constants.CELLERY_CREATE_GCP:
    {
        // Prompt yes or no
    }
default:
    {
        Back()
    }
}

有没有类似的优雅提示方式?

试试这个 func yesNo() bool:

package main

import (
    "fmt"
    "log"

    "github.com/manifoldco/promptui"
)

func main() {
    fmt.Println(yesNo())
    fmt.Println(yesNo())

}
func yesNo() bool {
    prompt := promptui.Select{
        Label: "Select[Yes/No]",
        Items: []string{"Yes", "No"},
    }
    _, result, err := prompt.Run()
    if err != nil {
        log.Fatalf("Prompt failed %v\n", err)
    }
    return result == "Yes"
}

输出:

? Select[Yes/No]: 
  ▸ Yes
    No

✔ Yes
true
✔ No
false