golang讀取ini配置,推薦使用第三方庫 go-ini
go get gopkg.in/ini.v1
package utilsimport ( "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.gopackage mainimport ( "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: 3333ip: 127.0.0.1, port: 2202, user_name :test1