package main import ( "flag" "fmt" ) var ( root = flag.String("root", "/var/lib/docker/fakevol", "fake volumes root directory") ) func main() { wordPtr := flag.String("word", "foo", "a string") numbPtr := flag.Int("numb", 42, "an int") boolPtr := flag.Bool("fork", false, "a bool") var svar string flag.StringVar(&svar, "svar", "bar", "a string var") flag.Parse() fmt.Println("word:", *wordPtr) fmt.Println("numb:", *numbPtr) fmt.Println("fork:", *boolPtr) fmt.Println("svar:", svar) fmt.Println("tail:", flag.Args()) }
执行 go build mytest.go
./mytest -word=1 -numb=2 -fork=true -svar=4 11111111
输出结果:
word: 1
numb: 2
fork: true
svar: 4
tail: [11111111] (其它参数)
有疑问加站长微信联系(非本文作者)