<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 解析命令行flag標識 使用flag包可以輕松地將命令行標識參數添加到Go應用程序。它有一些缺點——你往往需要復制很多代碼以添加簡寫版本的標志,并且它們是按幫助提示的字母順序排序的。 有許多第三方庫試圖解決這些缺點,但本章將重點關注標準庫版本而不是那些第三方庫。 ### 實踐 1. 創建flags.go: ``` package main import ( "flag" "fmt" ) // Config存儲接收到的標識 type Config struct { subject string isAwesome bool howAwesome int countTheWays CountTheWays } // Setup 根據傳入的標識初始化配置 func (c *Config) Setup() { // 你可以使用這樣的方式直接初始化標識: // var someVar = flag.String("flag_name", "default_val", "description") // 但在實際操作中使用結構體來承載會更好一些 // 完整版 flag.StringVar(&c.subject, "subject", "", "subject is a string, it defaults to empty") // 簡寫版 flag.StringVar(&c.subject, "s", "", "subject is a string, it defaults to empty (shorthand)") flag.BoolVar(&c.isAwesome, "isawesome", false, "is it awesome or what?") flag.IntVar(&c.howAwesome, "howawesome", 10, "how awesome out of 10?") // 自定義變量類型 flag.Var(&c.countTheWays, "c", "comma separated list of integers") } // GetMessage 將所有的內部字段拼接成完整的句子 func (c *Config) GetMessage() string { msg := c.subject if c.isAwesome { msg += " is awesome" } else { msg += " is NOT awesome" } msg = fmt.Sprintf("%s with a certainty of %d out of 10. Let me count the ways %s", msg, c.howAwesome, c.countTheWays.String()) return msg } ``` 2. 建立custom.go: ``` package main import ( "fmt" "strconv" "strings" ) // CountTheWays使一個自定義變量類型 // 我們會從標識中讀取到它 type CountTheWays []int // 想要實現自定義類型的flag 必須實現flag.Value接口 // 該接口包含了 // String() string // Set(string) error func (c *CountTheWays) String() string { result := "" for _, v := range *c { if len(result) > 0 { result += " ... " } result += fmt.Sprint(v) } return result } func (c *CountTheWays) Set(value string) error { values := strings.Split(value, ",") for _, v := range values { i, err := strconv.Atoi(v) if err != nil { return err } *c = append(*c, i) } return nil } ``` 3. 建立main.go: ``` package main import ( "flag" "fmt" ) func main() { // 初始化 c := Config{} c.Setup() // 常見的調用方式 flag.Parse() // 將通過命令行輸入的flag標識拼接打印 fmt.Println(c.GetMessage()) } ``` 4. 將main.go打包為flags,運行會顯示: ``` is NOT awesome with a certainty of 10 out of 10. Let me count the ways ``` 5. 運行./flags -s會顯示: ``` flag needs an argument: -s Usage of ./flags: -c value comma separated list of integers -howawesome int how awesome out of 10? (default 10) -isawesome is it awesome or what? -s string subject is a string, it defaults to empty (shorthand) -subject string subject is a string, it defaults to empty ``` 6. 運行./flags -c 1,2,3 -s aaa會顯示: ``` aaa is NOT awesome with a certainty of 10 out of 10. Let me count the ways 1 ... 2 ... 3 ``` ### 說明 本節演示了flag包的大多數常見用法。涉及對自定義變量類型,各種內置變量,長短標識以及結構映射標識的使用。我們需要main函數以調用flag.Parse()。 在這個示例中你會發現能夠自動獲取-h以顯示包含的標識列表。需要注意的是,可以在沒有參數的情況下調用的布爾標識,并且標識的順序無關緊要。 flag包提供了快速構建命令行應用的方法,在工作中我們可以利用其來指定設置日志級別或根據應用程序的需求引導用戶輸入。在命令行參數章節中,我們將探索標識集并使用參數在它們之間切換。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看