golang中讀取ini配置

來源:互聯網
上載者:User

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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.