配置文件/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)