<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 12.4.1 os包 os包中有一個string類型的切片變量`os.Args`,其用來處理一些基本的命令行參數,它在程序啟動后讀取命令行輸入的參數。來看下面的打招呼程序: 示例 12.11?[os_args.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_12/os_args.go): ~~~ // os_args.go package main import ( "fmt" "os" "strings" ) func main() { who := "Alice " if len(os.Args) > 1 { who += strings.Join(os.Args[1:], " ") } fmt.Println("Good Morning", who) } ~~~ 我們在IDE或編輯器中直接運行這個程序輸出:`Good Morning Alice` 我們在命令行運行`os_args or ./os_args`會得到同樣的結果。 但是我們在命令行加入參數,像這樣:`os_args John Bill Marc Luke`,將得到這樣的輸出:`Good Morning Alice John Bill Marc Luke` 這個命令行參數會放置在切片`os.Args[]`中(以空格分隔),從索引1開始(`os.Args[0]`放的是程序本身的名字,在本例中是`os_args`)。函數`strings.Join`函數用來連接這些參數,以空格作為間隔。 **練習 12.5**:[hello_who.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_12/hello_who.go)?寫一個"Hello World"的變種程序:把人的名字作為程序命令行執行的一個參數,比如:`hello_who Evan Michael Laura`?那么會輸出`Hello Evan Michael Laura`! ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/12.4.md#1242-flag包)12.4.2 flag包 flag包有一個擴展功能用來解析命令行選項。但是通常被用來替換基本常量,例如,在某些情況下我們希望在命令行給常量一些不一樣的值。(參看19章的項目) 在flag包中一個Flag被定義成一個含有如下字段的結構體: ~~~ type Flag struct { Name string // name as it appears on command line Usage string // help message Value Value // value as set DefValue string // default value (as text); for usage message } ~~~ 下面的程序`echo.go`模擬了Unix的echo功能: ~~~ package main import ( "flag" // command line option parser "os" ) var NewLine = flag.Bool("n", false, "print newline") // echo -n flag, of type *bool const ( Space = " " Newline = "\n" ) func main() { flag.PrintDefaults() flag.Parse() // Scans the arg list and sets up flags var s string = "" for i := 0; i < flag.NArg(); i++ { if i > 0 { s += " " if *NewLine { // -n is parsed, flag becomes true s += Newline } } s += flag.Arg(i) } os.Stdout.WriteString(s) } ~~~ `flat.Parse()`掃描參數列表(或者常量列表)并設置flag,?`flag.Arg(i)`表示第i個參數。`Parse()`之后所有`flag.Arg(i)`全部可用,`flag.Arg(0)`就是第一個真實的flag,而不是像`os.Args(o)`放置程序的名字。 `flag.Narg()`返回參數的數量。解析后flag或常量就可用了。`flag.Bool()`定義了一個默認值是`false`的flag:當在命令行出現了第一個參數(這里是"n"),flag被設置成'true'(NewLine是`*bool`類型)。如果`*NewLine`表示對flag解引用,所以當值是`true`時將添加一個newline。 `flag.PrintDefaults()`打印flag的使用幫助信息,本例中打印的是: ~~~ -n=false: print newline ~~~ `flag.VisitAll(fn func(*Flag))`是另一個有用的功能:按照字典順序遍歷flag,并且對每個標簽調用fn(參考15.8章的例子) 當在命令行(Windows)中執行:`echo.exe A B C`,將輸出:`A B C`;執行`echo.exe -n A B C`,將輸出: ~~~ A B C ~~~ 每個字符的輸出都新起一行,每次都在輸出的數據前面打印使用幫助信息:`-n=false: print newline` 對于`flag.Bool`你可以設置布爾型flag來測試你的代碼,例如定義一個flag?`processedFlag`: ~~~ var processedFlag = flag.Bool(“proc”, false, “nothing processed yet”) ~~~ 在后面用如下代碼來測試: ~~~ if *processedFlag { // found flag -proc r = process() } ~~~ 要給flag定義其它類型,可以使用`flag.Int()`,`flag.Float64`,`flag.String()` 在15.8章你將找到一個完成的例子。
                  <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>

                              哎呀哎呀视频在线观看