1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        「GoCN酷Go推薦」交互式命令行工具庫survey

        共 5643字,需瀏覽 12分鐘

         ·

        2021-10-02 03:06


        ☆ 什么是 survey? ☆

        survey 可以讓你方便的在終端上構(gòu)建交互式和可訪問提示的應(yīng)用,支持ANSI

        ☆ 安裝 ☆

        go get github.com/AlecAivazis/survey/v2

        ☆ 快速開始

        package main

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

        // the questions to ask
        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: "red",
                },
            },
            {
                Name: "age",
                Prompt:   &survey.Input{Message: "How old are you?"},
            },
        }

        func main() {
            // 結(jié)果寫入到結(jié)構(gòu)體
            answers := struct {
                Name          string                  // survey 會(huì)默認(rèn)匹配首字母小寫的name
                FavoriteColor string `survey:"color"` // 或者你也可以用tag指定如何匹配
                Age           int                     // 如果類型不一致,survey會(huì)嘗試轉(zhuǎn)換
            }{}

            // 執(zhí)行提問
            err := survey.Ask(qs, &answers)
            if err != nil {
                fmt.Println(err.Error())
                return
            }

            fmt.Printf("%s chose %s.", answers.Name, answers.FavoriteColor)
        }


        IMG1

        ☆ 文本輸入

        name := ""
        prompt := &survey.Input{
            Message: "ping",
        }
        survey.AskOne(prompt, &name)

        ☆ 多行輸入

        text := ""
        prompt := &survey.Multiline{
            Message: "ping",
        }
        survey.AskOne(prompt, &text)

        ☆ 密碼輸入

        password := ""
        prompt := &survey.Password{
            Message: "Please type your password",
        }
        survey.AskOne(prompt, &password)

        ☆ 確認(rèn)

        name := false
        prompt := &survey.Confirm{
            Message: "Do you like pie?",
        }
        survey.AskOne(prompt, &name)

        ☆ 單選

        color := ""
        prompt := &survey.Select{
            Message: "Choose a color:",
            Options: []string{"red""blue""green"},
        }
        survey.AskOne(prompt, &color)

        ☆ 多選

        days := []string{}
        prompt := &survey.MultiSelect{
            Message: "What days do you prefer:",
            Options: []string{"Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"},
        }
        survey.AskOne(prompt, &days)

        ☆ 驗(yàn)證 

        q := &survey.Question{
            Prompt: &survey.Input{Message: "Hello world validation"},
            Validate: func (val interface{}) error {
                // 自定義驗(yàn)證
                if str, ok := val.(string) ; !ok || len(str) > 10 {
                    return errors.New("This response cannot be longer than 10 characters.")
                }
                return nil
            },
        }

        color := ""
        prompt := &survey.Input{ Message: "Whats your name?" }

        survey.AskOne(prompt, &color, survey.WithValidator(survey.Required))

        survey內(nèi)置了幾種Validators

        • Required 要求必填
        • MinLength(n) 輸入字符最小長度驗(yàn)證
        • MaxLength(n) 輸入字符最大長度驗(yàn)證
        • MaxItems(n) 選項(xiàng)最多驗(yàn)證
        • MinItems(n) 選項(xiàng)最小驗(yàn)證

        ☆ 總結(jié) 

        survey可以幫助我們快速開發(fā)出交互式命令行應(yīng)用,使用簡(jiǎn)單,功能強(qiáng)大,文檔齊全,歡迎使用

        ☆ 參考鏈接

        https://github.com/AlecAivazis/survey

        《酷Go推薦》招募:


        各位Gopher同學(xué),最近我們社區(qū)打算推出一個(gè)類似GoCN每日新聞的新欄目《酷Go推薦》,主要是每周推薦一個(gè)庫或者好的項(xiàng)目,然后寫一點(diǎn)這個(gè)庫使用方法或者優(yōu)點(diǎn)之類的,這樣可以真正的幫助到大家能夠?qū)W習(xí)到

        新的庫,并且知道怎么用。


        大概規(guī)則和每日新聞?lì)愃疲绻麍?bào)名人多的話每個(gè)人一個(gè)月輪到一次,歡迎大家報(bào)名!戳「閱讀原文」,即可報(bào)名


        掃碼也可以加入 GoCN 的大家族喲~



        瀏覽 103
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            91久久精品国产免天美传媒 | 午夜a视频 | 妲己无圣光无遮挡 | 亚洲欧美精品suv | 粉嫩人妻 | www老鸭窝 | 免费的黄色视频 | 国产成人免费电影 | 三级片大香蕉 | 亚裔evelynlin毛片 |