Read INI configuration in Golang

Source: Internet
Author: User
Tags key string

Golang Read INI configuration, it is recommended to use a third-party library Go-ini

    • Installation
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
    
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.