packagemain import( "flag" "fmt" ) var( root=flag.String("root","/var/lib/docker/fakevol","fakevolumesrootdirectory") ) funcmain(){ wordPtr:=flag.String("word","foo","astring") numbPtr:=flag.Int("numb",42,"anint") boolPtr:=flag.Bool("fork",false,"abool") varsvarstring flag.StringVar(&svar,"svar","bar","astringvar") 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] (其它参数)