【学习笔记】golang第三方库goconfig的使用
如何获得goconfig第三方包
在git shell中使用go get github.com/Unknwon/goconfig安装goconfig包
如何使用goconfig
package main import ( "fmt" "github.com/Unknwon/goconfig" "log" ) func main() { cfg, err := goconfig.LoadConfigFile("config.ini") if err != nil { log.Println("读取配置文件失败[conf.ini]") return } str, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "default_key") fmt.Printf("%s\n", str) }
goconfig包提供如下函数:
func SaveConfigFile(c *ConfigFile, filename string) (err error)
func LoadConfigFile(fileName string, moreFiles ...string) (c *ConfigFile, err error)
func (c *ConfigFile) Bool(section, key string) (bool, error)
func (c *ConfigFile) Float64(section, key string) (float64, error)
func (c *ConfigFile) GetKeyComments(section, key string) (comments string)
func (c *ConfigFile) GetSection(section string) (map[string]string, error)
func (c *ConfigFile) GetSectionComments(section string) (comments string)
func (c *ConfigFile) GetValue(section, key string) (string, error)
func (c *ConfigFile) Int64(section, key string) (int64, error)
func (c *ConfigFile) MustBool(section, key string, defaultVal ...bool) bool
func (c *ConfigFile) MustFloat64(section, key string, defaultVal ...float64) float64
func (c *ConfigFile) MustInt(section, key string, defaultVal ...int) int
func (c *ConfigFile) MustInt64(section, key string, defaultVal ...int64) int64
func (c *ConfigFile) MustValue(section, key string, defaultVal ...string) string
func (c *ConfigFile) SetKeyComments(section, key, comments string) bool
func (c *ConfigFile) SetSectionComments(section, comments string) bool
func (c *ConfigFile) SetValue(section, key, value string) bool
总的来说,goconfig包基本满足对ini等配置文件读取需求,当然该包还提供很多其他的特性,进一步的学习,请查看
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。