go配置文件toml使用

配置文件/home/amber/db.toml:

# This is a TOML document.

title = "TOML Example"

[database]
server = "192.10.92.10"
ports = [ 5000, 5001, 5002 ]
connection_max = 5000
enabled = true   //bool類型

go文件

type tomlConfig struct {
	Title string
	DB database `toml:"database"`
}
type database struct {
	Server  string `toml:"server"`
	Ports   []int  `toml:"ports"`
	ConnMax int    `toml:"connection_max"`
	Enabled bool   `toml:"enabled"`
}

// 通過toml.DecodeFile將配置文件所在路徑和struct結合在一起
var config tomlConfig
filePath := "/home/amber/db.toml"
if _, err := toml.DecodeFile(filePath, &config); err != nil {
	panic(err)
}

if config.DB.Enabled {
	fmt.Sprintf("db enable: %v", config.DB.Enabled)
}
fmt.Sprintf("db Server : %v", config.DB.Server)
fmt.Sprintf("db Ports: %v", config.DB.Ports)
fmt.Sprintf("db ConnMax: %v", config.DB.ConnMax)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章