Golang 配置文件相关操作

本文以读取数据库配置文件为例

1、JSON 文件

 package main​/*解析 json 格式的配置文件文件内容如下:{"type": "json","postgres": {"host": "localhost","port": 5432,"username": "postgres","password": "postgres","dbname": "bubble"}}*/import ("encoding/json""fmt""io/ioutil""os")​// 定义第一级配置文件的结构体type Config struct {Type     stringPostgres DbConf // 数据类型为第二级配置文件的结构体名称}​// 定义第二级配置文件的结构体   注意数据类型type DbConf struct {Host     string `json:"host"`Port     uint   `json:"port"`Username string `json:"username"`Password string `json:"password"`DbName   string `json:"dbname"`}​// 定义配置文件结构体type JsonStruct struct {}​// 创建配置文件的构造函数func NewJsonStruct() *JsonStruct {return &JsonStruct{}}​// 加载配置文件func (jt *JsonStruct) Load(filename string, v interface{}) {// 读取配置文件data, err := ioutil.ReadFile(filename)if err != nil {return}// 解析配置文件err = json.Unmarshal(data, v)if err != nil {return}​}​func main() {JsonParse := NewJsonStruct()v := Config{}// 获取配置文件路径osGwd, _ := os.Getwd()confPath := osGwd + "/conf_json.json"// 加载配置文件JsonParse.Load(confPath, &v)fmt.Printf("配置文件的类型为 %s \n", v.Type)fmt.Printf("PG 数据库的配置为 %s \n", v.Postgres)}​

2、YAML 文件(推荐)

 package main/*解析 yaml 格式的配置文件文件内容如下:database:postgres:host: localhostport: 5432username: postgrespassword: postgresdbname: bubble} */​import ("fmt""gopkg.in/yaml.v2""io/ioutil""os")​type YamlStruct struct {}​func NewYamlStruct() *YamlStruct {return &YamlStruct{}}​type YamlConfig struct {DataBase DataBase `yaml:"database"`}​type DataBase struct {PgConf PgConf `yaml:"postgres"`}​type PgConf struct {Host     string `yaml:"host"`Port     string `yaml:"port"`Username string `yaml:"username"`Password string `yaml:"password"`DbName   string `yaml:"dbname"`}​func (yam *YamlStruct) Load(filename string, v interface{}) {data, err := ioutil.ReadFile(filename)if err != nil {panic(err.Error())}err = yaml.Unmarshal(data, v)if err != nil {panic(err.Error())}}​func main() {YamlStruct := NewYamlStruct()v := YamlConfig{}osGwd, _ := os.Getwd()confPath := osGwd + "/conf_yaml.yaml"YamlStruct.Load(confPath, &v)fmt.Printf("配置文件的数据为 %s \n", v.DataBase)}​

3、INI 文件

 package main​/*解析 ini 格式的配置文件文件内容如下:mode=debug​[postgres]host=localhostport=5432username=postgrespassword=postgresdbname=bubble*/​import ("fmt""github.com/go-ini/ini""os")​//type Postgres struct {//  Host     string//  Port     uint//  Username string//  Password string//  DbName   string//}​func main() {osGwd, _ := os.Getwd()confPath := osGwd + "/conf_ini.ini"config, err := ini.Load(confPath)if err != nil {panic(err.Error())}// 可以直接读取配置,并设置默认值mode := config.Section("").Key("mode").MustString("debug")fmt.Println(mode)postgres, err := config.GetSection("postgres")if err != nil {panic(err.Error())}// 可通过 Key 去取值fmt.Println(postgres.Key("host"))  // localhostfmt.Println(postgres.Keys())       //  [localhost 5432 postgres postgres bubble]fmt.Println(postgres.KeyStrings()) // [host port username password dbname]}​

Golang 多种配置文件解析相关推荐

  1. viper4android io错误,golang常用库之配置文件解析库-viper使用详解

    一.viper简介 viper 配置管理解析库,是由大神 Steve Francia 开发,他在google领导着 golang 的产品开发,他也是 gohugo.io 的创始人之一,命令行解析库 c ...

  2. Golang配置文件解析-oozgconf

    代码地址如下: http://www.demodashi.com/demo/14411.html 简介 oozgconf基于Golang开发,用于项目中配置文件的读取以及加载,是一个轻量级的配置文件工 ...

  3. mybatis3 配置文件解析

    mybatis3 配置文件解析 2013-05-08 19:43 34388人阅读 评论(0) 收藏 举报 分类: mybatis3(19) 目录(?)[+] 配置文件的基本结构 configurat ...

  4. Spring Boot(17)配置文件解析

    Spring Boot(17)配置文件解析 前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此外还内置了一个 ...

  5. Redis docker安装及redis.conf配置文件解析

    Redis docker安装及配置文件解析 目录 Redis docker安装及配置文件解析 安装 配置文件编写 基础配置 开发配置 全局其他配置 Redis实现分布式锁 基本原理 实现思路 主要流程 ...

  6. 【Nginx源码分析】Nginx配置文件解析(一)

    运营研发团队 李乐 配置文件是nginx的基础,对于学习nginx源码甚至开发nginx模块的同学来说更是必须深究.本文将从源码从此深入分析nginx配置文件的解析,配置存储,与配置查找. 看本文之前 ...

  7. mybatis配置文件解析

    mybatis配置文件解析 mybatis核心配置文件`mybatis-config.xml文件. mybatis的配置文件包含了会深深影响mybatis行为的设置和属性信息. 能配置的内容: con ...

  8. 【c语言】C语言配置文件解析库——iniparser

    转载自:http://blog.csdn.net/u011192270/article/details/49339071 C语言配置文件解析库--iniparser 前言:在对项目的优化时,发现Lin ...

  9. C语言配置文件解析库——iniparser

    C语言配置文件解析库--iniparser 1. 1.1前言:在对项目的优化时,发现Linux下没有专门的供给C语言使用的配置文件函数,于是搜索到了iniparser库,可以像那些面向对象语言一样,使 ...

最新文章

  1. 第十章 优先级队列 (b1)完全二叉堆:结构
  2. 移动端网页fixed布局问题解决方案
  3. WebSocket客户端连接断开后,服务器端的析构处理
  4. python之numpy
  5. eq linux_音乐家和音乐爱好者的开放硬件 | Linux 中国
  6. div.php织梦自定义表判断不能为空,织梦使用if判断某个字段是否为空
  7. java netty modbus协议接收iot数据
  8. Ubuntu16下载tomcat8
  9. 5款 Mac 常用PDF阅读和编辑软件推荐
  10. 超好用的数学教学软件:几何画板Sketchpad for Mac中文版
  11. 机器学习备忘录 | 二分类模型常用评价指标汇总
  12. 免费顺丰快递单号查询API接口demo【快递鸟API接口】
  13. [电脑驱动向]笔记本键盘失灵,电脑插耳机没反应,不要着急拿去物理维修,可能是bios驱动需要更新
  14. Incapsula js加密混淆分析
  15. Java面试题大全带答案 40道
  16. openwrt修改logo
  17. 通过简单的类和接口实现手机套餐办理服务
  18. less 使用入门教程
  19. ACM,IEEE ,Elsevier和Springer旗下期刊
  20. SLCP认证辅导,SLCP认证产品应覆盖不同的产品类别

热门文章

  1. 图片:网络红人代言保健酒 回归还是从良?
  2. 一文弄懂Flink基础理论
  3. 猎豹移动财报:营收下降厄运下,求生技能满分
  4. 公务员碑文辞职书走红,以教堂碑文为铭改变生活
  5. matlab求解微分方程的解析解
  6. 一个查询手机号码归属地,运营商,区号信息的 Go 库
  7. 物联网 + 区块链系列(二):区块链赋能物联网设备
  8. Spring Boot 的aplication.properties 中文乱码问题
  9. 业绩回暖背后,华润啤酒高端战略路线道阻且艰
  10. python设置单元格宽度_Python-docx设置表格列宽度