内容简介:给go程序添加命令行参数
需要应用程序根据不同的配置文件访问不同的服务器,希望程序启动时可以指定配置文件。
解决方案
package main
import (
"os"
"fmt"
)
func main() {
argsWithProg := os.Args
if(len(argsWithProg) < 2) {
fmt.Println("usage : ",argsWithProg[0]," configFile")
return
}
confFile := os.Args[1]
fmt.Println(argsWithProg)
fmt.Println(confFile)
}
运行效果如下:
[root@local t1]# go build argTest1.go [root@local t1]# ./argTest1 usage : ./argTest1 configFile [root@local t1]# ./argTest1 default.xml [./argTest1 default.xml] default.xml [root@local t1]#
讨论
这里只是个简单的示例代码,其它功能请自行扩展。
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/ 20170610_给 go 程序添加命令行参数. rst
欢迎补充
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Charlotte's Web
E. B. White / Scholastic / 2004 / USD 0.01
This is the tale of how a little girl named Ferm, with the help of a friendly spider, saved her pig, Wilbur, from the usual fate of nice fat little pigs.一起来看看 《Charlotte's Web》 这本书的介绍吧!