golang的flag包

前端之家收集整理的这篇文章主要介绍了golang的flag包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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] (其它参数)

猜你在找的Go相关文章