You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 2 Current »
AdapterContent
AdapterContent是基于配置内容的实现,用户可以给定具体的配置内容,生成Adapter接口对象。
Adapter
大部分场景下,我们可以通过框架已经封装好的g.Cfg单例对象来便捷使用基于文件的配置管理实现。例如:
package main import ( "fmt" "github.com/gogf/gf/v2/os/gcfg" "github.com/gogf/gf/v2/os/gctx" ) const content = ` server: address: ":8888" openapiPath: "/api.json" swaggerPath: "/swagger" dumpRouterMap: false database: default: link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test" debug: true ` func main() { var ctx = gctx.New() adapter, err := gcfg.NewAdapterContent(content) if err != nil { panic(err) } config := gcfg.NewWithAdapter(adapter) fmt.Println(config.MustGet(ctx, "server.address").String()) fmt.Println(config.MustGet(ctx, "database.default").Map()) }
运行后,终端输出:
:8888 map[debug:true link:mysql:root:12345678@tcp(127.0.0.1:3306)/test]