如何获得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等配置文件读取需求,当然该包还提供很多其他的特性,进一步的学习,请查看
有无闻提供的学习视频http://www.ucai.cn/course/chapter/134/3701/6833
有疑问加站长微信联系(非本文作者)