toml的使用示例

1.編寫配置文件 xx.toml

[email]
host="smtp.mxhichina.com"
port=465
username="[email protected]"
password="xxxxx"
toUsers = [ "[email protected]" ]

[server]
port=8777

[mysql]
dsn = "root:password@tcp(127.0.0.1:3306)/xxx?charset=utf8&parseTime=True&loc=Local&interpolateParams=true"
maxIdleConn = 8
maxOpenConn = 16
maxLifetime = 600

2.編寫go文件讀取配置

package conf

import "github.com/BurntSushi/toml"

var Conf = &config{}

func init() {
    file := "conf/xx.toml"
    if _, err := toml.DecodeFile(file, Conf); err != nil {
        panic(err)
    }
}

type config struct {
    Server *Server `toml:"server"`
    Email *Email `toml:"email"`
}

type Server struct {
    Port int64 `toml:"port"`
}

type Email struct {
    Host string `toml:"host"`
    Port int `toml:"port"`
    UserName string `toml:"username"`
    Password string `toml:"password"`
    ToUsers []string `toml:"toUsers"`
}

type Mysql struct {
    Dsn string `toml:"dsn"`
    MaxIdleConn int `toml:"maxIdleConn"`
    MaxOpenConn int `toml:"maxOpenConn"`
    MaxLifetime int  `toml:"maxLifetime"`
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章