golang读取ini配置,推荐使用第三方库 go-ini
- 安装
go get gopkg.in/ini.v1
- 测试代码
简单封装下
package utils
import (
"gopkg.in/ini.v1"
)
type IniParser struct {
conf_reader *ini.File // config reader
}
type IniParserError struct {
error_info string
}
func (e *IniParserError) Error() string { return e.error_info }
func (this *IniParser) Load(config_file_name string) error {
conf, err := ini.Load(config_file_name)
if err != nil {
this.conf_reader = nil
return err
}
this.conf_reader = conf
return nil
}
func (this *IniParser) GetString(section string, key string) string {
if this.conf_reader == nil {
return ""
}
s := this.conf_reader.Section(section)
if s == nil {
return ""
}
return s.Key(key).String()
}
func (this *IniParser) GetInt32(section string, key string) int32 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_int, _ := s.Key(key).Int()
return int32(value_int)
}
func (this *IniParser) GetUint32(section string, key string) uint32 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_int, _ := s.Key(key).Uint()
return uint32(value_int)
}
func (this *IniParser) GetInt64(section string, key string) int64 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_int, _ := s.Key(key).Int64()
return value_int
}
func (this *IniParser) GetUint64(section string, key string) uint64 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_int, _ := s.Key(key).Uint64()
return value_int
}
func (this *IniParser) GetFloat32(section string, key string) float32 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_float, _ := s.Key(key).Float64()
return float32(value_float)
}
func (this *IniParser) GetFloat64(section string, key string) float64 {
if this.conf_reader == nil {
return 0
}
s := this.conf_reader.Section(section)
if s == nil {
return 0
}
value_float, _ := s.Key(key).Float64()
return value_float
}
测试代码
// config_read_test project main.go
package main
import (
"fmt"
"utils/ini"
)
func main() {
fmt.Println("config read test!")
ini_parser := utils.IniParser{}
conf_file_name := "conf.ini"
if err := ini_parser.Load("conf.ini"); err != nil {
fmt.Printf("try load config file[%s] error[%s]\n", conf_file_name, err.Error())
return
}
ip := ini_parser.GetString("", "test.ip")
port := ini_parser.GetInt64("", "port")
user_name := ini_parser.GetString("", "user_name")
item_1 := ini_parser.GetInt64("", "item1")
item_2 := ini_parser.GetInt64("", "item2")
fmt.Printf("ip: %s, port: %d, user_name :%s, item1: %d, item2: %d\n", ip, port, user_name, item_1, item_2)
ip = ini_parser.GetString("test", "ip")
port = ini_parser.GetInt64("test", "port")
user_name = ini_parser.GetString("test", "user_name")
fmt.Printf("ip: %s, port: %d, user_name :%s\n", ip, port, user_name)
}
运行结果
config read test!
ip: , port: 0, user_name :, item1: 0, item2: 3333
ip: 127.0.0.1, port: 2202, user_name :test1
有疑问加站长微信联系(非本文作者)