1.下载依赖
go get -u github.com/go-ini/ini
2.创建conf.ini文件
//conf/app.ini配置文件
[database]
Type = mysql
User = root
Password =
Host = 127.0.0.1:3306
Name =
3.在setting中配置参数
type Database struct {
Type string
User string
Password string
Host string
Name string
}
var cfg *ini.File
var DatabaseSetting =&Database{}
cfg,_ =ini.Load("conf/app.ini")
cfg.Section("database").MapTo(DatabaseSetting)
4.使用配置文件
sec:= setting.DatabaseSetting
fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
sec.User,
sec.Password,
sec.Host,
sec.Name)
对以后修改参数比较方便