代码地址如下:
http://www.demodashi.com/demo/14411.html

简介

oozgconf基于Golang开发,用于项目中配置文件的读取以及加载,是一个轻量级的配置文件工具。

功能

  1. 配置文件读取
  2. 配置文件解析

支持配置文件格式

  • .json
  • .toml
  • .xml
  • .yaml

安装

$ go get -u github.com/usthooz/oozgconf

实现思路

在后端项目中,配置文件已经是一个不可或缺的东西了,格式也是多种多样。

流程结构

如下图所示为项目实现流程及结构:

代码目录结构

主要代码

  • 配置文件后缀名常量定义
const (
JsonSub string = "json"
YamlSub string = "yaml"
TomlSub string = "toml"
XmlSub string = "xml"
)
  • 对象结构
type OozGconf struct {
// ConfPath config file path->default: ./config/config.yaml
ConfPath string
// Subffix config file subffix
Subffix string
}
  • 新建gconf对象

    在使用时,如果不指定配置文件的路径,那么默认为./config/config.yaml,同时如果不指定文件类型,则自动通过解析文件名来获得配置文件的后缀。
// NewConf new conf object
func NewConf(confParam *OozGconf) *OozGconf {
if len(confParam.ConfPath) == 0 {
confParam.ConfPath = "./config/config.yaml"
}
return confParam
}
  • 获取配置
/*
confpath: config file path->default: ./config/config.yaml
subffix: config file subffie->option
*/
func (oozConf *OozGconf) GetConf(conf interface{}) error {
// read config file
bs, err := ioutil.ReadFile(oozConf.ConfPath)
if err != nil {
return err
}
if len(oozConf.Subffix) == 0 {
// get file subffix
oozConf.Subffix = strings.Trim(path.Ext(path.Base(oozConf.ConfPath)), ".")
}
// check analy
switch oozConf.Subffix {
case JsonSub:
err = json.Unmarshal(bs, conf)
case TomlSub:
err = toml.Unmarshal(bs, conf)
case YamlSub:
err = yaml.Unmarshal(bs, conf)
case XmlSub:
err = xml.Unmarshal(bs, conf)
default:
err = fmt.Errorf("GetConf: non support this file type...")
}
return err
}

使用例程

  • example
import (
"github.com/usthooz/oozgconf"
"github.com/usthooz/oozlog/go"
) type Config struct {
Author string
Mysql struct {
User string
Password string
}
} func main() {
var (
conf Config
)
// new conf object
ozconf := oozgconf.NewConf(&oozgconf.OozGconf{
ConfPath: "./config.json", // 可选,默认为./config/config.yaml
Subffix: "", // 可选,如果不指定则自动解析文件名获取
})
// get config
err := ozconf.GetConf(&conf)
if err != nil {
uoozg.Errorf("GetConf Err: %v", err.Error())
}
uoozg.Infof("Res: %v", conf)
}

运行结果

其他

没有

Golang配置文件解析-oozgconf

代码地址如下:
http://www.demodashi.com/demo/14411.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

Golang配置文件解析-oozgconf的更多相关文章

  1. golang开发:类库篇(四)配置文件解析器goconfig的使用

    为什么要使用goconfig解析配置文件 目前各语言框架对配置文件书写基本都差不多,基本都是首先配置一些基础变量,基本变量里面有环境的配置,然后通过环境变量去获取该环境下的变量.例如,生产环境跟测试环 ...

  2. MyBatis配置文件解析

    MyBatis配置文件解析(概要) 1.configuration:根元素 1.1 properties:定义配置外在化 1.2 settings:一些全局性的配置 1.3 typeAliases:为 ...

  3. Nginx安装与配置文件解析

    导读 Nginx是一款开放源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3代理服务,是一款自由的软件,同时也是运维工程师必会的一种服务器,下面我就简单的说一下Nginx服务器的 ...

  4. Hadoop配置文件解析

    Hadoop源码解析 2 --- Hadoop配置文件解析 1 Hadoop Configuration简介    Hadoop没有使用java.util.Properties管理配置文件, 也没有使 ...

  5. Python3 配置文件 解析

    /************************************************************************ * Python3 配置文件 解析 * 说明: * ...

  6. Hibernate的配置文件解析

    配置mybatis.xml或hibernate.cfg.xml报错: <property name="connection.url">jdbc:mysql://loca ...

  7. WCF中配置文件解析

    WCF中配置文件解析[1] 2014-06-14 WCF中配置文件解析 参考 WCF中配置文件解析 返回 在WCF Service Configuration Editor的使用中,我们通过配置工具自 ...

  8. haproxy之配置文件解析

    功能--> 提供高可用/负载均衡/基于tcp和http应用的代理;支持虚拟主机,特别适用于负载特大的web站点. 配置文件解析--> #配置文件-->开启/proc/net/ipv4 ...

  9. nginx源代码分析--配置文件解析

    ngx-conf-parsing 对 Nginx 配置文件的一些认识: 配置指令具有作用域,分为全局作用域和使用 {} 创建其他作用域. 同一作用域的不同的配置指令没有先后顺序:同一作用域能否使用同样 ...

随机推荐

  1. 编码策略:在ios编码中一定要少写全局变量。

    ios中全局变量默认是灰绿色的,只有少些全局变量,才能提高代码的聚合程度.才能更容易管理代码.

  2. TPL中限制进程数量

    The MaxDegreeOfParallelism sets the maximum number of simultaneous threads that will be used for the ...

  3. mr

    大数据技术 —— MapReduce 简介 本文为senlie原创,转载请保留此地址:http://www.cnblogs.com/senlie/ 1.概要很多计算在概念上很直观,但由于输入数据很大, ...

  4. cocos2d-x 安卓环境配置 -转

    win7+cocos2d-x-2.1.4+vs2012环境太简单就不多说了,下面是eclipse环境 一.准备 1.eclipse+adt+sdk:adt-bundle-windows-x86_64- ...

  5. Struts2 S标签 数目字格式化成金额输出(保留两位小数)

    JSP: <s:property value="%{formatDouble(price)}" /> Action:添加 //格式化数字显示 public String ...

  6. Android之开源中国客户端源码分析(一)

    程序启动第一个界面类: net.oschina.app.AppStart功能描述:一张图片代码细节描述:一个透明度的动画效果,效果动画完成后自动启动新的Activity(Main) 基本BaseAct ...

  7. linux下so获得自己文件位置的路径

    打开这个设备/proc/self/maps 返回的就是这个进程当前使用的so列表 cat /proc/self/maps00400000-0040b000 r-xp 00000000 08:01 14 ...

  8. [ GIT ] GIT tip : A simple .gitconfig file

    reference : http://fle.github.io/git-tip-a-simple-gitconfig-file.html As several friends have asked ...

  9. 2012年及之后的ImageNet比赛的冠军、亚军和季军ImageNet winners after 2012

    2012 0.15 - Supervision (AlexNet) - ~ 60954656 params 0.26 - ISI (ensemble of features) 0.27 - LEAR ...

  10. kafka存储机制

    kafka存储机制 @(博客文章)[storm|大数据] kafka存储机制 一关键术语 二topic中partition存储分布 三 partiton中文件存储方式 四 partiton中segme ...