7

golang中读取ini配置

 4 years ago
source link: https://studygolang.com/articles/14224?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang中读取ini配置

yandaren · 2018-08-15 11:35:02 · 3917 次点击 · 预计阅读时间 3 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2018-08-15 11:35:02 的文章,其中的信息可能已经有所发展或是发生改变。


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

有疑问加站长微信联系(非本文作者)

280

本文来自:简书

感谢作者:yandaren

查看原文:golang中读取ini配置

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK