给go程序添加命令行参数

栏目: Go · 发布时间: 6年前

内容简介:给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

欢迎补充


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Bandit Algorithms for Website Optimization

Bandit Algorithms for Website Optimization

John Myles White / O'Reilly Media / 2013-1-3 / USD 19.99

This book shows you how to run experiments on your website using A/B testing - and then takes you a huge step further by introducing you to bandit algorithms for website optimization. Author John Myle......一起来看看 《Bandit Algorithms for Website Optimization》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具