• Golang配置文件解析-oozgconf


    代码地址如下:
    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大师代发,拒绝转载,转载需要作者授权

  • 相关阅读:
    使用JDBC连接并操作数据库
    JDBC连接数据库的url设useSSL参数为true产生的问题
    0-1背包问题的学习及LeetCode相关习题练习
    MySQL---操作数据库
    6、Python Requests库高级操作【2】
    5、Python Requests库高级操作【1】
    4、Python 数据解析【2】
    3、Python 数据解析【1】
    Python正则re.S,re.I等作用
    2、Python 使用Requests库通用爬取数据操作
  • 原文地址:https://www.cnblogs.com/demodashi/p/10474080.html
Copyright © 2020-2023  润新知