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] (其它参数)
时间: 2024-10-08 13:11:31