gofream默认配置文件config.toml
test.toml
# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[database]
[[database.default]]
host = "127.0.0.1"
port = "3306"
user = "root"
pass = "123456"
name = "test1"
type = "mysql"
role = "master"
charset = "utf8"
priority = "1"
[[database.default]]
host = "127.0.0.1"
port = "3306"
user = "root"
pass = "123456"
name = "test2"
type = "mysql"
role = "master"
charset = "utf8"
priority = "1"
# Redis数据库配置
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
package main
import (
"fmt"
_ "gf-app/boot"
_ "gf-app/router"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcfg"
)
func main() {
// 设置配置文件
g.Cfg().SetFileName("test.toml")
// 全局获取配置
fmt.Println(g.Cfg().Get("redis.disk"))
fmt.Println(g.Cfg().Get("redis"))
fmt.Println(g.Cfg().GetString("database.default.1.host"))
fmt.Println(g.Cfg().GetInt("database.default.0.port"))
// 单例模式
cfg := gcfg.Instance()
cfg.SetFileName("test.toml")
// 获取单例模式配置
fmt.Println(cfg.Get("redis.disk"))
fmt.Println(cfg.Get("redis"))
fmt.Println(cfg.GetString("database.default.1.host"))
fmt.Println(cfg.GetInt("database.default.0.port"))
}
网友评论