golang下使用ini配置文件(widuu/goini)
在“widuu/goini”基础上进行了修改,增加了其他数据类型配置值(string、int、int32、int64、[]int、[]string)的支持。
使用方法:
ConfigCentor := goini.SetConfig("./config.ini")
读取int配置值:ConfigCentor.GetValueInt("ES","LogLevel")
读取string配置值:ConfigCentor.GetValue("ES","Url")
读取int数组配置值(","为分隔符):ConfigCentor.GetValueArray("ES","Url")
源码lib包(包名:goini,在go的src下创建目录goini,创建conf.go文件放在此目录即可):
package goini import (
"bufio"
"fmt"
"io"
"os"
"strings"
"strconv"
) type Config struct {
filepath string
conflist []map[string]map[string]string
} //Create empty file
func SetConfig(filepath string) *Config {
c := new(Config)
c.filepath = filepath return c
} //key values:string
func (c *Config) GetValue(section, name string) string {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
return value[name]
}
}
}
return ""
} //key values:int
func (c *Config) GetValueInt(section, name string) int {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
val,_ := strconv.Atoi(value[name])
return val
}
}
}
return 0
} //key values:int
func (c *Config) GetValueInt32(section, name string) int32 {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
val,_:=strconv.ParseInt(value[name],10,32)
return int32(val)
}
}
}
return 0
} //key values:int
func (c *Config) GetValueInt64(section, name string) int64 {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
val,_:=strconv.ParseInt(value[name],10,64)
return val
}
}
}
return 0
} //key values:[]int,split by ","
func (c *Config) GetValueArray(section, name string) []string {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
arr := strings.Split(value[name], ",")
return arr
}
}
}
return nil
} //key values:[]int,split by ","
func (c *Config) GetValueIntArray(section, name string) []int {
c.ReadList()
conf := c.ReadList()
for _, v := range conf {
for key, value := range v {
if key == section {
arr := strings.Split(value[name], ",")
arrValue := []int{}
for _, str := range arr {
val,_:=strconv.Atoi(str)
arrValue=append(arrValue,val)
}
return arrValue
}
}
}
return nil
} //Set the corresponding value of the key value, if not add, if there is a key change
func (c *Config) SetValue(section, key, value string) bool {
c.ReadList()
data := c.conflist
var ok bool
var index = make(map[int]bool)
var conf = make(map[string]map[string]string)
for i, v := range data {
_, ok = v[section]
index[i] = ok
} i, ok := func(m map[int]bool) (i int, v bool) {
for i, v := range m {
if v == true {
return i, true
}
}
return 0, false
}(index) if ok {
c.conflist[i][section][key] = value
return true
} else {
conf[section] = make(map[string]string)
conf[section][key] = value
c.conflist = append(c.conflist, conf)
return true
} return false
} //Delete the corresponding key values
func (c *Config) DeleteValue(section, name string) bool {
c.ReadList()
data := c.conflist
for i, v := range data {
for key, _ := range v {
if key == section {
delete(c.conflist[i][key], name)
return true
}
}
}
return false
} //List all the configuration file
func (c *Config) ReadList() []map[string]map[string]string { file, err := os.Open(c.filepath)
if err != nil {
CheckErr(err)
}
defer file.Close()
var data map[string]map[string]string
var section string
buf := bufio.NewReader(file)
for {
l, err := buf.ReadString('\n')
line := strings.TrimSpace(l)
if err != nil {
if err != io.EOF {
CheckErr(err)
}
if len(line) == 0 {
break
}
}
switch {
case len(line) == 0:
case line[0] == '[' && line[len(line)-1] == ']':
section = strings.TrimSpace(line[1 : len(line)-1])
data = make(map[string]map[string]string)
data[section] = make(map[string]string)
default:
i := strings.IndexAny(line, "=")
value := strings.TrimSpace(line[i+1 : len(line)])
data[section][strings.TrimSpace(line[0:i])] = value
if c.uniquappend(section) == true {
c.conflist = append(c.conflist, data)
}
} } return c.conflist
} func CheckErr(err error) string {
if err != nil {
return fmt.Sprintf("Error is :'%s'", err.Error())
}
return "Notfound this error"
} //Ban repeated appended to the slice method
func (c *Config) uniquappend(conf string) bool {
for _, v := range c.conflist {
for k, _ := range v {
if k == conf {
return false
}
}
}
return true
}
golang下使用ini配置文件(widuu/goini)的更多相关文章
- Python读取ini配置文件的方式
python configparser模块 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- DCMTK开源库的学习笔记4:利用ini配置文件对dcm影像进行归档
转:http://blog.csdn.net/zssureqh/article/details/8846337 背景介绍: 医学影像PACS工作站的服务端需要对大量的dcm文件进行归档,写入数据库处理 ...
- c#读写ini配置文件示例
虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧 其他人写的都是调用非托管kernel32.dll.我也用过 ...
- VC++/MFC操作ini配置文件详解
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...
- python 提供INI配置文件的操作 ConfigParser
原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...
- 第四十二节,configparser特定格式的ini配置文件模块
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...
- MySQL的my-innodb-heavy-4G.ini配置文件的翻译
我根据MySQL配置文件的英文文档说明,在根据自己所学的知识,使用有道词典对不懂的单词进行了查询,一个一个翻译出来的.有的专业术语翻译的不好,我使用了英文进行标注,例如主机(master)和副机(sl ...
- python读取uti-8格式ini配置文件出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 367: illegal multibyte sequence错误解决方法
出现这种错误只需要在read下添加encoding='utf-8' 如: from configparser import ConfigParser cf = ConfigParser() cf.re ...
- INI配置文件的格式
为什么要用INI文件?如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序出厂后还能根据需要进行必要 ...
随机推荐
- 搞不懂的算法-排序篇<2>
上一篇排序算法<1>中,排序算法的时间复杂度从N2到NlgN变化,但他们都有一个共同的特点,基于比较和交换数组中的元素来实现排序,我们称这些排序算法为比较排序算法.对于比较排序算法,所有的 ...
- 反射API提供的常用类和函数
ReflectionParameter 取回了函数或方法参数的相关信息. {//要自行检查函数的参数,首先创建一个 ReflectionFunction 或 ReflectionMethod 的 实例 ...
- 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
这是在复制代码的时候,没有修改路径,但是IDEA没有报错,还会爆出 WARN ework.web.servlet.PageNotFound - No mapping found for HTTP re ...
- Vue学习之路第十二篇:为页面元素设置内联样式
1.有了上一篇的基础,接下来理解内联样式的设置会更简单一点,先看正常的css内联样式: <dvi id="app"> <p style="font-si ...
- JAVA中各个包的主要作用
00:48:0800:48:1022013013-06-282013-06-2800:48:182013-06-2800:48:20 java.util是JAVA的utility工具包 java.l ...
- HDU 4513 吉哥系列故事——完美队形II( Manacher变形 )
链接:传送门 思路:根据完美队形的定义,可以得知,完美队形实质上是 回文串 + 序列出现峰,因为是在回文串中再次增加了一个要求,所以可以对 Manacher 进行改造,改造的部分应该为暴力匹配的循环 ...
- 会话cookie和持久化cookie实现session
当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set- Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSE ...
- JavaScript模块化编程之AMD
简单的说一下AMD是"Asynchronous Module Definition"的缩写,意思就是"异步模块定义".它采用异步方式加载模块,模块的加载不影响它 ...
- 免费ftp服务器FileZilla Server配置
FileZilla Server下载安装完成后,必须启动软件进行设置,由于此软件是英文,本来就是一款陌生的软件,再加上英文,配置难度可想而知,小编从网上找到一篇非常详细的教程进行整理了一番,确保读到这 ...
- Qt QImage与OpenCV Mat转换
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51029382 应一个朋友的要求,整理总 ...