教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Golang配置文件解析-oozgconf

Golang配置文件解析-oozgconf

发布时间:2022-01-23   编辑:jiaochengji.com
教程集为您提供Golang配置文件解析-oozgconf等资源,欢迎您收藏本站,我们将为您提供最新的Golang配置文件解析-oozgconf资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg><blockquote>

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

</blockquote> <h3>简介</h3>

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

<h3>
功能</h3> <ol><li>配置文件读取</li><li>配置文件解析</li></ol><h3>支持配置文件格式</h3> <ul><li>.json</li><li>.toml</li><li>.xml</li><li>.yaml</li></ul><h3>安装</h3> <pre><code>$ go get -u github.com/usthooz/oozgconf </code></pre> <h3>实现思路</h3>

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

<h4>
流程结构</h4>

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

<h4>
代码目录结构</h4>

<h4>
主要代码</h4> <ul><li>配置文件后缀名常量定义</li></ul><pre><code>const ( JsonSub string = "json" YamlSub string = "yaml" TomlSub string = "toml" XmlSub string = "xml" ) </code></pre> <ul><li>对象结构</li></ul><pre><code>type OozGconf struct { // ConfPath config file path->default: ./config/config.yaml ConfPath string // Subffix config file subffix Subffix string } </code></pre> <ul><li>新建gconf对象
在使用时,如果不指定配置文件的路径,那么默认为./config/config.yaml,同时如果不指定文件类型,则自动通过解析文件名来获得配置文件的后缀。</li></ul><pre><code>// NewConf new conf object func NewConf(confParam *OozGconf) *OozGconf { if len(confParam.ConfPath) == 0 { confParam.ConfPath = "./config/config.yaml" } return confParam } </code></pre> <ul><li>获取配置</li></ul><pre><code>/* 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 } </code></pre> <h3>
使用例程</h3> <ul><li>example</li></ul><pre><code>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) } </code></pre> <h3>运行结果</h3>

<h3>
其他</h3>

没有
Golang配置文件解析-oozgconf

<blockquote>

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

</blockquote> <blockquote>

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

</blockquote> 到此这篇关于“Golang配置文件解析-oozgconf”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Golang配置文件解析-oozgconf
golang json文件存取
Golang 配置文件热加载
golang web框架 配置文件读取 借鉴 beego
Go语言从入门到精通 -【web项目实战篇】- 读取配置文件
golang开源配置管理组件-go-archaius介绍
golang之配置文件
golang读取配置文件
golang基础教程
如何读取yaml,json,ini等配置文件【Golang 入门系列九】

[关闭]
~ ~