Golang Read INI configuration, it is recommended to use a third-party library Go-ini
go get gopkg.in/ini.v1
- Test code
Simple package under
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 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 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 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_rea Der = = Nil {return 0} s: = This.conf_reader. 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 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_reade r = = Nil {return 0} s: = This.conf_reader. 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 if s = = nil {return 0} value_float, _: = S.key (Key). Float64 () return value_float}
Test code
// 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)}
Run results
Config read test!ip:, port:0, user_name:, item1:0, item2:3333ip:127.0.0.1, port:2202, user_name:test1