Home > Article > Backend Development > An article will guide you through the use of flag package in Go language
flag
package. #The purpose of this package is to receive command line parameters.
If you have playedMysql
, you must be familiar with this interface
Yes, that’s what it’s used for, receiving command line parameters.
Any code learned in this chapter must be compiled into .exe
can run, prohibit right click run
! !
In fact Go The os module also has this function, but this function is somewhat lacking.
Cannot receive a specification like -u root -p 3306 The value of key
, can only receive the value like root 3306
. Let’s take a brief look.
Code
func main() { cmdArgs := os.Args if len(cmdArgs) > 0 { for index, value := range cmdArgs { fmt.Printf("索引:%v,value值:%v\n", index, value) } } }
We pass the commandgo build -o mysql. exe main.go
is compiled into a mysql.exe
.
##mysql.exe diagram
Execution results
##Note:Returned cmdArgs is a slice, the first value is its own file name , if you don’t want your first value, just slice it directly.
line 2 code. 执行结果 上述的可能也发现问题了,只能接收简单的值,不能接收 flag可以做到,flag有只能接收以下几种类型。 使用flag包,最后要使用 话不多说,直接代码 执行结果 .exe -h结果图 懂了叭,你今天,学废了吗! 上述你可能也发现了问题,需要用 执行结果 flag还有一些其他无关紧要的方法,了解就好。 执行结果 The main thing to learn in this chapter is the With this we can develop programs like ##flag.TypeVarcmdArgs := os.Args[1:]
flag
key value
这种。
bool
int
Series(int
, int64
, uint
, uint64
)float
series(float
,float64
)string
duration
注意
flag.Parse()
转换一下才能获取命令行参数。flag.Type()
func main() {
// 第一个参数是命令行key,第二个参数是默认是,第三个参数是 .exe -h 提示
var user = flag.String("user", "root", "用户名")
var port = flag.Int("port", 3306, "端口")
var ip = flag.String("ip", "localhost", "mysql ip")
//必须使用flag.Parse()解析一下命令行参数
flag.Parse()
//flag.Type返回的是一个指针,必须通过 *变量取值
fmt.Println(*user,*port,*ip)
}
flag.TypeVar()
*变量
才能取到值,是不是感觉不太方便,那就来看看flag.TypeVar()
。func main() {
//声明变量用于接收命令行参数
var user string
var port int
var ip string
//从命令行扫描参数赋值到变量
flag.StringVar(&user, "user", "root", "用户名")
flag.IntVar(&port, "port", 3306, "端口")
flag.StringVar(&ip, "ip", "localhost", "mysql ip")
//必须使用flag.Parse()解析一下命令行参数
flag.Parse()
//flag.Type返回的是一个指针,必须通过 *变量取值
fmt.Println(user, port, ip)
}
flag其他方法
func main() {
//声明变量用于接收命令行参数
var user string
var port int
var ip string
//从命令行扫描参数赋值到变量
flag.StringVar(&user, "user", "root", "用户名")
flag.IntVar(&port, "port", 3306, "端口")
flag.StringVar(&ip, "ip", "localhost", "mysql ip")
//必须使用flag.Parse()解析一下命令行参数
flag.Parse()
//flag.Type返回的是一个指针,必须通过 *变量取值
fmt.Println(user, port, ip)
///////////////// 其他方法
//返回命令行参数后的其他参数
fmt.Println(flag.Args())
//返回命令行参数后的其他参数个数
fmt.Println(flag.NArg())
//返回使用的命令行参数个数
fmt.Println(flag.NFlag())
}
Summary
flag
package Use, #flag
package mainly handles the command line parameter passing problem. mysql
. flag
Mainly include flag.Type
and flag.TypeVar
Two differences
flag.Type
The variable coming out is a pointer type, you need to use *Variable name
can take the valueThe variable that comes out is directly the standard variable (recommend)
The above is the detailed content of An article will guide you through the use of flag package in Go language. For more information, please follow other related articles on the PHP Chinese website!