Hello World程序實例
Go變量實例
Go常量實例
Go for循環語句實例
Go if/else語句實例
Go switch語句實例
Go切片實例
Go範圍實例
Go函數實例
Go函數多個返回值實例
Go可變參數的函數實例
Go閉包(匿名函數)實例
Go函數遞歸實例
Go指針實例
Go指針實例
Go接口實例
Go錯誤實例
Go程序實例
Go通道實例
Go通道緩衝實例
Go通道同步實例
Go通道路線實例
Go Select實例
Go超時(timeouts)實例
Go非阻塞通道操作實例
Go關閉通道實例
Go通道範圍實例
Go計時器實例
Go斷續器實例
Go工作池實例
Go速率限制實例
Go原子計數器實例
Go互斥體實例
Go有狀態的goroutines實例
Go排序實例
Go按自定義函數排序實例
Go panic錯誤處理實例
Go延遲(defer)實例
Go集合函數實例
Go字符串函數實例
Go字符串格式化實例
Go正則表達式實例
Go JSON實例
Go時間日期實例
Go時代(Epoch)實例
Go時間格式化/解析實例
Go隨機數實例
Go數字解析實例
Go URL解析實例
Go SHA1哈希實例
Go Base64編碼實例
Go讀取文件實例
Go寫文件實例
Go行過濾器實例
Go命令行參數實例
Go命令行標誌實例
Go環境變量實例
Go執行過程實例
Go信號實例
Go退出程序實例
Go命令行標誌實例
命令行標誌是指定命令行程序選項的常用方法。 例如,在wc -l
中,-l
是命令行標誌。Go提供了一個支持基本命令行標誌解析的標誌包。這裏將使用這個包來實現示例命令行程序。
所有的示例代碼,都放在
F:\worksp\golang
目錄下。安裝Go編程環境請參考:http://www.yiibai.com/go/go\_environment.html
command-line-flags.go
的完整代碼如下所示 -
package main
// Go provides a `flag` package supporting basic
// command-line flag parsing. We'll use this package to
// implement our example command-line program.
import "flag"
import "fmt"
func main() {
// Basic flag declarations are available for string,
// integer, and boolean options. Here we declare a
// string flag `word` with a default value `"foo"`
// and a short description. This `flag.String` function
// returns a string pointer (not a string value);
// we'll see how to use this pointer below.
wordPtr := flag.String("word", "foo", "a string")
// This declares `numb` and `fork` flags, using a
// similar approach to the `word` flag.
numbPtr := flag.Int("numb", 42, "an int")
boolPtr := flag.Bool("fork", false, "a bool")
// It's also possible to declare an option that uses an
// existing var declared elsewhere in the program.
// Note that we need to pass in a pointer to the flag
// declaration function.
var svar string
flag.StringVar(&svar, "svar", "bar", "a string var")
// Once all flags are declared, call `flag.Parse()`
// to execute the command-line parsing.
flag.Parse()
// Here we'll just dump out the parsed options and
// any trailing positional arguments. Note that we
// need to dereference the pointers with e.g. `*wordPtr`
// to get the actual option values.
fmt.Println("word:", *wordPtr)
fmt.Println("numb:", *numbPtr)
fmt.Println("fork:", *boolPtr)
fmt.Println("svar:", svar)
fmt.Println("tail:", flag.Args())
}
執行上面代碼,將得到以下輸出結果 -
F:\worksp\golang>go build command-line-flags.go
F:\worksp\golang>command-line-flags -word=opt -numb=7 -fork -svar=flag
word: opt
numb: 7
fork: true
svar: flag
tail: []
F:\worksp\golang>command-line-flags -word=opt
word: opt
numb: 42
fork: false
svar: bar
tail: []
F:\worksp\golang>command-line-flags -word=opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3]
F:\worksp\golang>command-line-flags -word=opt a1 a2 a3 -numb=7
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3 -numb=7]
F:\worksp\golang>command-line-flags -h
Usage of command-line-flags:
-fork
a bool
-numb int
an int (default 42)
-svar string
a string var (default "bar")
-word string
a string (default "foo")
F:\worksp\golang>command-line-flags -wat
flag provided but not defined: -wat
Usage of command-line-flags:
-fork
a bool
-numb int
an int (default 42)
-svar string
a string var (default "bar")
-word string
a string (default "foo")