Go语言学习之flag包(The way to go)
生命不止,继续 go go go !!! 今天跟大家分享的是golang中的flag package,如果你在golang中用到了命令行参数,那么你就必须要了解一些flag的知识。 Package flag 定义flags func (f *FlagSet) String(name string,value string,usage string) *string
String defines a string flag with specified name,default value,and usage string. The return value is the address of a string variable that stores the value of the flag. 定义int flag: func Int64(name string,value int64,usage string) *int64
Int64 defines an int64 flag with specified name,and usage string. The return value is the address of an int64 variable that stores the value of the flag. 定义布尔flag: func Bool(name string,value bool,usage string) *bool
Bool defines a bool flag with specified name,and usage string. The return value is the address of a bool variable that stores the value of the flag. 这里唯一指的注意的就是返回值:是指针 应用示例: import "flag"
var ip = flag.Int("flagname", 1234,"help message for flagname")
将flag绑定到一个变量 func StringVar(p *string,name string,usage string)
StringVar defines a string flag with specified name,and usage string. The argument p points to a string variable in which to store the value of the flag. func IntVar(p *int,value int,usage string)
IntVar defines an int flag with specified name,and usage string. The argument p points to an int variable in which to store the value of the flag. func BoolVar(p *bool,usage string)
BoolVar defines a bool flag with specified name,and usage string. The argument p points to a bool variable in which to store the value of the flag. 应用示例: var flagvar int
func init() {
flag.IntVar(&flagvar,"flagname","help message for flagname")
}
绑定自定义的类型 flag.Var(&flagVal,"name","help message for flagname")
For such flags,the default value is just the initial value of the variable. 将命令行参数解析到flag中 func Parse()
Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program. NArg和NFlag NFlag returns the number of command-line flags that have been set. 获取非flag参数 func Arg(i int) string
Arg returns the i’th command-line argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist. func Args() []string
Args returns the non-flag command-line arguments. 如何区分non-flag和flag 应用 package main
import (
"flag"
"fmt"
)
var Input_Name = flag.String("name","dabaojian","input your name")
var Input_Age = flag.Int("age", 20,"input your age")
var Input_Gender = flag.String("gender","male","input your age")
var Input_flagvar int
func Init() {
flag.IntVar(&Input_flagvar,"help message for flagname")
}
func main() {
Init()
flag.Parse()
fmt.Printf("args=%s,num=%dn",flag.Args(),flag.NArg())
fmt.Printf("flag num=%dn",flag.NFlag())
for i := 0; i != flag.NArg(); i++ {
fmt.Printf("arg[%d]=%sn",i,flag.Arg(i))
}
fmt.Println("name=",*Input_Name)
fmt.Println("age=",*Input_Age)
fmt.Println("gender=",*Input_Gender)
fmt.Println("flagname=",Input_flagvar)
}
build: go build test_flag.go
执行: 输出: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |