• 你好!欢迎你的到来
  • 关于我们
  • 首页 博客 学习笔记 技术导航 工具
  • 博文分类
    • PHP(43)
    • MySQL(11)
    • Linux(28)
    • html(3)
    • JQuery(4)
    • JavaScript(9)
    • svn(2)
    • CSS(2)
    • seajs(1)
    • go(44)
    • redis(1)
    • nginx(8)
    • mongo(0)
    • es(0)
    • 算法(0)
    • 其他(26)
    • 生活(1)
    专栏
    • Jquery基础教程
      • 文章:(15)篇
      • 阅读:17006
    • shell命令
      • 文章:(42)篇
      • 阅读:58050
    • Git教程
      • 文章:(36)篇
      • 阅读:114513
    • leetCode刷题
      • 文章:(37)篇
      • 阅读:14417
    • 摘要视图
    • 目录视图
    使用BurntSushi/toml处理项目中toml格式的配置文件
    2019-03-06 21:48 阅读(3133) 评论(0)


    一、简介

    配置文件有很多种类,比如.ini、xml、json、YAML再到TOML,TOML 的全称是 Tom's Obvious, Minimal Language,因为它的作者是 GitHub 联合创始人 Tom Preston-Werner。TOML 的目标是成为一个极简的配置文件格式。TOML 被设计成可以无歧义地被映射为哈希表,从而被多种语言解析。

    说白了,就是将指定格式的配置文件中的内容,映射到某个变量中,从而通过这个变量取访问配置文件中的内容。

    github:https://github.com/toml-lang/toml

    关于TOML的一些规范

    1、OML是大小写敏感的

    2、TOML文件必须是UTF8编码的

    3、空白符可以是制表符(0x09)或空格(0x20)

    4、换行符可以是 LF (0x0A) 或 CRLF (0x0D0A)


    在最开始接触go语言的时候,配置文件,我往往都是通过json.Unmarshal进行关联映射。如下:

    假设配置文件如下:

    {
      "debug": true,
      "db": {
        "dsn": "root:test@tcp(127.0.0.1:3363)/findme",
        "maxIdle": 30
      }
    }

    先来搞一下,如果不其他组件,我们需要自己读取配置文件,通过json.Unmarshal进行关联映射。如下:

    config/conf.go

    package config
    
    import (
       "log"
       "encoding/json"
       "github.com/toolkits/file"
    )
    
    type GlobalConfig struct {
       Debug bool `json:"debug"`
       DataBase DataBase `json:"db"`
    }
    
    type DataBase struct {
       DNS string `json:"dsn"`
       MaxIdle int `json:"maxIdle"`
    }
    var gConfig * GlobalConfig
    
    func GetConfig() *GlobalConfig {
       return  gConfig
    }
    
    func ParseConf(filePath string)  {
       if filePath == "" {
          log.Fatalln("use -c to specify configuration file")
       }
    
       if !file.IsExist(filePath) {
          log.Fatalln("config file:", filePath, "is not existent")
       }
    
       fileContent, err :=file.ToString(filePath)
       if err != nil {
          log.Fatalln("read config file:", filePath, "fail:", err)
       }
    
       err = json.Unmarshal([]byte(fileContent), &gConfig)
       if err != nil {
          log.Fatalln("parse config file:", filePath, "fail:", err)
       }
    }

    调用如下:

    package main
    
    import (
       "fmt"
       "flag"
       "test/config"
    )
    
    func main() {
       configFile := flag.String("c", "config/conf.toml", "configuration file")
       flag.Parse()
       config.ParseConf(*configFile)
        
       fmt.Println(config.GetConfig().DataBase) //打印数据库信息   
    }

    二、案例

    如果我们需要使用的配置文件,还是上面内容,此时,需要我们将其转为toml格式的,如下:

    #是否开启调试模式
    debug = true
    
    #数据库的配置
    [db]
    dns = "root:test@tcp(127.0.0.1:3363)/findme"
    maxIdle = 30

    映射配置文件的程序如下:

    package config
    
    import (
       "log"
       "github.com/BurntSushi/toml"
       "sync"
    )
    
    var lock = new(sync.RWMutex)
    
    type GlobalConfig struct {
       Debug    bool `toml:"debug"`
       DataBase DataBase `toml:"db"`
    }
    
    type DataBase struct {
       DNS     string `toml:"dns"`
       MaxIdle int `toml:"maxIdle"`
    }
    
    var gConfig *GlobalConfig
    
    func GetConfig() *GlobalConfig {
       lock.RLock() //加读锁,防止读的时候,被写
       defer lock.RUnlock()  //读完解锁
       
       return gConfig
    }
    
    func ParseConf(filePath string) {
       lock.RLock()
       defer lock.RUnlock()
       
       if filePath == "" {
          log.Fatalln("use -c to specify configuration file")
       }
       _, err := toml.DecodeFile(filePath, &gConfig)
       if err != nil {
          log.Println("toml.DecodeFile error")
          panic(err)
       }
    }


    调用和上面保持一致即可,运行结果如下:

    {root:test@tcp(127.0.0.1:3363)/findme 30}

    注意事项:若配置文件是json格式,我们在结构体的tags中使用了json,配置文件是toml格式的,我们在结构体的tags中使用了toml。



    本文为原创文章,请尊重辛勤劳动,如需转载,请保留本文地址
    http://www.findme.wang/blog/detail/id/581.html

    若您感觉本站文章不错,读后有收获,不妨赞助一下?

    我要赞助

    您还可以分享给朋友哦

    更多
    顶
    2
    踩
    0
    • 上一篇: php中长文本折叠问题
    • 下一篇: 公钥和私钥哪些事
    • 查看评论
    • 正在加载中...
    • 留言
    • 亲,您还没有登录,登录后留言不需要审核哦!
      可以使用如下方式登录哦!
  • CSDN | 新浪微博 | github | 关于我们 | 我要留言 | 友链申请
  • 豫ICP备18038193号    Copyright ©lidequan All Rights Reserved