golang标准库flag包实现实现命令行解析
golang中flag包是用来对命令行参数解析的,看下面一个简单例子:
package main
import (
"fmt"
"flag"
)
func main() {
//使用flag.String()函数注册flag,得到结果conf指针
var conf = flag.String("f", "test.conf", "Specify the config file")
//也可以将flag绑定到某个变量上
var int_var int
flag.IntVar(&int_var, "i", 100, "just a test int")
//解析命令
flag.Parse();
fmt.Println(*conf)
fmt.Println(int_var)
}
运行效果:
mckee@mckee-thinkpad:~/program/go/test/src/test$ go run test.go -f /etc/test.conf -i 11111
/etc/test.conf
11111
您可能感兴趣的文章:
golang标准库flag包实现实现命令行解析
Go源码阅读之flag包
Golang学习日志 ━━ Go 常用包整理及介绍
[Introduction]匿名函数
Go语言实现HTTP压测工具(2)——Golang语言基础学习和使用
go语言标准库
golang和python有什么区别?
golang cli_使用golang构建基于cli的文件重命名工具
Golang包管理详解
【Golang源码分析】Golang如何实现自举(一)