Golang 多种配置文件解析
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 多种配置文件解析相关推荐
- viper4android io错误,golang常用库之配置文件解析库-viper使用详解
一.viper简介 viper 配置管理解析库,是由大神 Steve Francia 开发,他在google领导着 golang 的产品开发,他也是 gohugo.io 的创始人之一,命令行解析库 c ...
- Golang配置文件解析-oozgconf
代码地址如下: http://www.demodashi.com/demo/14411.html 简介 oozgconf基于Golang开发,用于项目中配置文件的读取以及加载,是一个轻量级的配置文件工 ...
- mybatis3 配置文件解析
mybatis3 配置文件解析 2013-05-08 19:43 34388人阅读 评论(0) 收藏 举报 分类: mybatis3(19) 目录(?)[+] 配置文件的基本结构 configurat ...
- Spring Boot(17)配置文件解析
Spring Boot(17)配置文件解析 前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此外还内置了一个 ...
- Redis docker安装及redis.conf配置文件解析
Redis docker安装及配置文件解析 目录 Redis docker安装及配置文件解析 安装 配置文件编写 基础配置 开发配置 全局其他配置 Redis实现分布式锁 基本原理 实现思路 主要流程 ...
- 【Nginx源码分析】Nginx配置文件解析(一)
运营研发团队 李乐 配置文件是nginx的基础,对于学习nginx源码甚至开发nginx模块的同学来说更是必须深究.本文将从源码从此深入分析nginx配置文件的解析,配置存储,与配置查找. 看本文之前 ...
- mybatis配置文件解析
mybatis配置文件解析 mybatis核心配置文件`mybatis-config.xml文件. mybatis的配置文件包含了会深深影响mybatis行为的设置和属性信息. 能配置的内容: con ...
- 【c语言】C语言配置文件解析库——iniparser
转载自:http://blog.csdn.net/u011192270/article/details/49339071 C语言配置文件解析库--iniparser 前言:在对项目的优化时,发现Lin ...
- C语言配置文件解析库——iniparser
C语言配置文件解析库--iniparser 1. 1.1前言:在对项目的优化时,发现Linux下没有专门的供给C语言使用的配置文件函数,于是搜索到了iniparser库,可以像那些面向对象语言一样,使 ...
最新文章
- 中国×××的“超级无敌”玩法
- JavaScript君,请您坦诚相待~~~
- 消息消费端的确认机制
- 差分约束系统之Bellman_Ford与Spfa判断负权回路
- [BZOJ3203][SDOI2013]保护出题人(凸包+三分)
- vue打卡日历_Vue日历
- [转载] python中for语句用法_详解Python中for循环的使用_python
- Python3标准库built-in、itertools、functools中的生成器
- 诺基亚自带36个铃声_vivo手机的6个隐藏功能,快开启,帮你玩转vivo
- 大数据为什么需要安全分析
- 行业报告归档 2019.2.8
- c语言 滑窗法_滑窗平均的另一种实现方式
- 上dnf一直连接服务器中,Win7系统下玩dnf提示正在连接服务器如何解决
- 矢量网络分析仪(Vector Network Analyzer)
- python pyecharts生成图表
- 手机显示仅限紧急呼叫 无服务 以及无信号的解决教程
- shp文件中polyline是什么_shp文件的读取
- 深入理解JVM虚拟机13:JVM面试题,看这篇就足够了(87题详解)
- 白鹭引擎王泽:重度H5游戏性能优化技巧标题的文章
- ea服务器和微软服务器,任天堂和微软竟然合作了?消息太过震撼,连推特服务器都挤爆了...