在使用开发过程中,会经常需要对配置文件做操作,目前主流的配置文件有 .ini、.yaml(yml)、.toml格式,本文收集一些用得较多的包做一个总汇记录。
使用https://github.com/go-ini/ini 包来操作ini文件
安装
go get gopkg.in/ini.v1
接口简单介绍
//加载文件
ini.Load(source interface{}, others ...interface{}) (*File, error)
//获取section 成功返回Setion结构体指针, 失败返回zero-value
ini.Section(name string) *Section
//s 未Section返回值, 使用string(), uint64, float64()等获取对应的值
s.Key(keyName).string()
例子
package optConfFile
import (
ini "gopkg.in/ini.v1"
)
//IniParser
type IniParser struct {
configReader *ini.File
}
//Load load ini file
func (i *IniParser) Load(configName string) error {
conf, err := ini.Load(configName)
if err != nil {
return err
}
i.configReader = conf
return nil
}
//GetString GetString
func (i *IniParser) GetString(section, key string) string {
if i.configReader == nil {
return ""
}
s := i.configReader.Section(section)
if s == nil {
return ""
}
return s.Key(key).String()
}
//GetUint64 GetUint64
func (i *IniParser) GetUint64(section, key string) uint64 {
if i.configReader == nil {
return 0
}
s := i.configReader.Section(section)
if s == nil {
return 0
}
v, _:= s.Key(key).Uint64()
return v
}
//GetFloat64 GetFloat64
func (i *IniParser) GetFloat64(section, key string) float64 {
if i.configReader == nil {
return 0
}
s := i.configReader.Section(section)
if s == nil {
return 0
}
v, _ := s.Key(key).Float64()
return v
}
使用github.com/Unknwon/goconfig
安装
go get github.com/Unknwon/goconfig
特性
- 简化流程,易于理解,更少出错。
- 提供与 Windows API 一模一样的操作方式。
- 支持读取递归节。
- 支持自增键名。
- 支持对注释的 读 和 写 操作,其它所有解析器都不支持!!!!
- 可以直接返回 bool, float64, int, int64 和 string 类型的值,如果使用 “Must” 开头的方法,则一定会返回这个类型的一个值而不返回错误,如果错误发生则会返回零值。
- 支持加载多个文件来重写值
例子
详细例子请参考GitHub上的示例
//只做出部分读配置信息的接口,更多接口的用法建议直接查看源码方式了解
//ErrLoadConfigFile
var ErrLoadConfigFile = errors.New("cloud not load config file")
//ConfigParser
type ConfigParser struct {
configFile *goconfig.ConfigFile
}
//Load load config file
func (c *ConfigParser) Load(configName string) error {
conf, err := goconfig.LoadConfigFile(configName)
if err != nil {
return err
}
c.configFile = conf
return nil
}
//GetSection
func (c *ConfigParser) GetSection(section string) (map[string]string, error) {
if c.configFile == nil {
return nil, ErrLoadConfigFile
}
return c.configFile.GetSection(section)
}
//GetString
func (c *ConfigParser) GetString(section, key string) (string, error) {
if c.configFile == nil {
return "", ErrLoadConfigFile
}
return c.configFile.GetValue(section, key)
}
//GetInt64
func (c *ConfigParser) GetInt64(section, key string) (int64, error) {
if c.configFile == nil {
return 0, ErrLoadConfigFile
}
return c.configFile.Int64(section, key)
}
//GetInt
func (c *ConfigParser) GetInt(section, key string) (int, error) {
if c.configFile == nil {
return 0, ErrLoadConfigFile
}
return c.configFile.Int(section, key)
}
//GetBool
func (c *ConfigParser) GetBool(section, key string) (bool, error) {
if c.configFile == nil {
return false, ErrLoadConfigFile
}
return c.configFile.Bool(section, key)
}
//MustString
func (c *ConfigParser) MustString(section, key string) string {
if c.configFile == nil {
return ""
}
return c.configFile.MustValue(section, key)
}
//GetSectionList
func (c *ConfigParser) GetSectionList() []string {
if c.configFile == nil {
return nil
}
return c.configFile.GetSectionList()
}
//GetKeyList
func (c *ConfigParser) GetKeyList(section string) []string {
if c.configFile == nil {
return nil
}
return c.configFile.GetKeyList(section)
}
//GetSectionComments
func (c *ConfigParser) GetSectionComments(section string) string {
if c.configFile == nil {
return ""
}
return c.configFile.GetSectionComments(section)
}
使用gopkg.in/yaml.v2 操作yaml文件
go get gopkg.in/yaml.v2
官方例子
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 2
d: [3, 4]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
A string
B struct {
RenamedC int `yaml:"c"`
D []int `yaml:",flow"`
}
}
func main() {
t := T{}
err := yaml.Unmarshal([]byte(data), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t:\n%v\n\n", t)
d, err := yaml.Marshal(&t)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t dump:\n%s\n\n", string(d))
m := make(map[interface{}]interface{})
err = yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m:\n%v\n\n", m)
d, err = yaml.Marshal(&m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m dump:\n%s\n\n", string(d))
}
使用github.com/jinzhu/configor
go get github.com/jinzhu/configor
官方例子
package main
import (
"fmt"
"github.com/jinzhu/configor"
)
var Config = struct {
APPName string `default:"app name"`
DB struct {
Name string
User string `default:"root"`
Password string `required:"true" env:"DBPassword"`
Port uint `default:"3306"`
}
Contacts []struct {
Name string
Email string `required:"true"`
}
}{}
func main() {
configor.Load(&Config, "config.yml")
fmt.Printf("config: %#v", Config)
}
使用github.com/BurntSushi/toml
安装
go get github.com/BurntSushi/toml
官方例子
example.toml文件
# This is a TOML document. Boom.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
代码
package main
import (
"fmt"
"time"
"github.com/BurntSushi/toml"
)
type tomlConfig struct {
Title string
Owner ownerInfo
DB database `toml:"database"`
Servers map[string]server
Clients clients
}
type ownerInfo struct {
Name string
Org string `toml:"organization"`
Bio string
DOB time.Time
}
type database struct {
Server string
Ports []int
ConnMax int `toml:"connection_max"`
Enabled bool
}
type server struct {
IP string
DC string
}
type clients struct {
Data [][]interface{}
Hosts []string
}
func main() {
var config tomlConfig
if _, err := toml.DecodeFile("example.toml", &config); err != nil {
fmt.Println(err)
return
}
fmt.Printf("Title: %s\n", config.Title)
fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
config.Owner.Name, config.Owner.Org, config.Owner.Bio,
config.Owner.DOB)
fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n",
config.DB.Server, config.DB.Ports, config.DB.ConnMax,
config.DB.Enabled)
for serverName, server := range config.Servers {
fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC)
}
fmt.Printf("Client data: %v\n", config.Clients.Data)
fmt.Printf("Client hosts: %v\n", config.Clients.Hosts)
}
还是使用上面的github.com/jinzhu/configor做解析,这里就不再举例了