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文件?如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序出厂后还能根据需要进行必要 ...
随机推荐
- UVa340未完成
#include<stdio.h> #define maxn 1010 int main() { ; while(scanf("%d",&num)!=EOF&a ...
- 提示 npm update check failed
执行npm命令时出现以下提示 虽然不影响代码运行,但总觉得看了很碍事, 查找资料后发现是因为文件夹权限的问题, .config / configstore文件夹中包含一个文件:update-notif ...
- 第一个TensorFlow程序
第一个TensorFlow程序 TensorFlow的运行方式分为如下4步: (1)加载数据及定义超参数 (2)构建网络 (3)训练模型 (4)评估模型和进行预测 import tensorflow ...
- jQuery中Ajax的几种写法
1. $.post(url,params,callback); 采用post方式提交,中文参数无需转码,在callback中如果要获取json字符串,还需转换一下. 2. $.getJSON(u ...
- 反射API提供的常用类和函数
ReflectionParameter 取回了函数或方法参数的相关信息. {//要自行检查函数的参数,首先创建一个 ReflectionFunction 或 ReflectionMethod 的 实例 ...
- jq DataTable
DataTables(http://datatables.club/index.html)应该是我到目前为止见过的,功能最强大的表格解决方案(当然,不计算其它整套框架中的table控件在内). 先把它 ...
- kernel对NTP的API,系统调用函数
kenrel API for NTP kernel 提供两个API(即系统调用 system call)给应用程序NTP,去校准kernel system clock Kernel Applicati ...
- 关于linux三种网络形式
今天是开始的第一天,额,没什么仪式.舍友偶然间提醒我,应该把学习的东西,做一下规划和整理.我想一想也是对的.所以开通了这个.希望以后回来可以看看自己曾经的幼稚,那证明了我不断在学习在进步.最近在准备C ...
- jQuery选择器练习中,带空格和不带空格的问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- struts配置
创建struts2的应用,首先应如前面所示要搭建好环境.jar包的导入和web.xml配置这里不在写出来. 如上所示,struts2中是采用<package>元素来管理Action的,包 ...