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
}

 

转载于:https://www.cnblogs.com/lijunhao/p/5952216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值