使用json而不是ini格式的配置文件;
对配置文件的解析不需要考虑内容,只要格式正确即可,需要配置的模块自行获取需要的字段。
首先需要获取配置文件路径,考虑两种方式:1.命令行参数提供;2.命令行参数没有提供时使用缺省文件。
-
var profile string
-
flag.StringVar(&profile, "profile", os.Getenv("GOETC")+"/profile.json", "Full path of the profile.")
- flag.Parse()
os包是平台独立的操作系统接口,比方说上面的Getenv,linux对应的就是getenv,可以看到参数是一致的。$GOETC在.bash_profile里面定义。说到这里,其实也可以把许多启动参数放到.bash_profile里面,不过这只是一种选择,就不讨论了。
flag包是专门用来解析命令行参数的。就StringVar函数来说,第一个参数用来保存读入的命令行参数,第二个参数是这个命令行参数的名字,第三个参数是缺省值,第四个参数是所谓的usage。
以上面程序为例,假设命令行参数为 -profile=./profile.json,那么变量profile在函数返回之后就是“./profile.json"这么个字符串,如果我们启动程序时命令行没有profile这么一项,则变量profile的值将会是$GOETC/profile.json。
我们可以用-h来获取所谓的usage,在这个程序里将打印的是
- Usage of mytchSever:
- -profile string
- Full path of the profile. (default "/home/whg/work/etc/profile.json")
点击(此处)折叠或打开
-
{
-
#no comments
-
"tcpPort":12345,
-
"httpPort":8080,
-
"httpsPort":436,
-
"others":{
-
"item1":"string",
-
"item2":false
-
}
- }
-
profileFD, err := os.Open(profile)
-
if err != nil {
-
panic(err)
-
}
-
defer profileFD.Close()
-
-
buffer := bufio.NewReader(profileFD)
-
var profileLines string
-
for {
-
line, err := buffer.ReadString('\n')
-
if err == io.EOF {
-
break
-
} else if line[0] == '#' || line[0] == ';' {
-
continue
-
} else if err != nil {
panic(err)
}
profileLines+= line
- }
说到这,需要说明下strings.Trim系列函数,刚开始以为是直接修改原字符串,看文档才发现结果是通过返回值返回的。
接下来,就是解析这个已经读出来的json串:
-
var ProfileItems map[string]interface{} //全局变量
-
jsonLines := []byte(profileLines)
-
-
if err := json.Unmarshal(jsonLines, &ProfileItems); err != nil {
-
panic(err)
-
}
-
httpPort := ProfileItems["httpPort"].(float64)
-
httpsPort := ProfileItems["httpsPort"].(float64)
-
tcpPort := ProfileItems["tcpPort"].(float64)
-
others := ProfileItems["others"].(map[string]interface{})
-
item1 := others["item1"].(string)
- item2 := others["item2"].(bool)
这是到目前为止的全部代码:
点击(此处)折叠或打开
-
package main
-
-
import (
-
"bufio"
-
"encoding/json"
-
"flag"
-
"fmt"
-
"io"
-
"os"
-
// "strings"
-
//"unicode/utf8"
-
)
-
-
var ProfileItems map[string]interface{} //obviously,this var will be used in many files
-
-
func main() {
-
-
var profile string
-
flag.StringVar(&profile, "profile", os.Getenv("GOETC")+"/profile.json", "Full path of the profile.")
-
flag.Parse()
-
-
profileFD, err := os.Open(profile)
-
if err != nil {
-
panic(err)
-
}
-
defer profileFD.Close()
-
-
buffer := bufio.NewReader(profileFD)
-
var profileLines string
-
for {
-
line, err := buffer.ReadString('\n')
-
if err == io.EOF {
-
break
-
} else if line[0] == '#' || line[0] == ';' {
-
continue
-
} else if err != nil {
-
panic(err)
-
}
-
-
profileLines += line
-
}
-
-
jsonLines := []byte(profileLines)
-
-
if err := json.Unmarshal(jsonLines, &ProfileItems); err != nil {
-
panic(err)
-
}
- }