golang json配置文件处理

  配置分两部分,系统启动参数使用文件,业务参数使用NoSQL类的内存数据库,这里只讨论文件,数据库留到将来再说;
  使用json而不是ini格式的配置文件;
  对配置文件的解析不需要考虑内容,只要格式正确即可,需要配置的模块自行获取需要的字段。
  首先需要获取配置文件路径,考虑两种方式:1.命令行参数提供;2.命令行参数没有提供时使用缺省文件。

var profile string
flag.StringVar(&profile, "profile", os.Getenv("GOETC")+"/profile.json", "Full path of the profile.")
flag.Parse()

  当然需要首先包含flag包和os包。
  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")

  设定配置文件内容是个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
    }

  需要包含io和bufio包。当然操作文件不仅仅只有bufio这种方式,既然是文本,按行读取终究是要简单些,况且,程序效率并不在于读取配置文件的方式上。
  说到这,需要说明下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)

  需要注意的一个是jsonLines是个用profileLines初始化的slice,另外一个是others域的处理,当然也都是很直白的处理。
  这是到目前为止的全部代码:

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)
    }
}

 

转载于:https://my.oschina.net/yiqu/blog/1593534

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值