AdapterContent

AdapterContent is an implementation based on configuration content, allowing users to generate an Adapter interface object by providing specific configuration content. The configuration content supports multiple formats, which are consistent with the configuration management component.

Usage Example

In most scenarios, we can conveniently use file-based configuration management through the g.Cfg singleton object already encapsulated by the framework. For example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gcfg"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. const content = `
  8. server:
  9. address: ":8888"
  10. openapiPath: "/api.json"
  11. swaggerPath: "/swagger"
  12. dumpRouterMap: false
  13. database:
  14. default:
  15. link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  16. debug: true
  17. `
  18. func main() {
  19. var ctx = gctx.New()
  20. adapter, err := gcfg.NewAdapterContent(content)
  21. if err != nil {
  22. panic(err)
  23. }
  24. config := gcfg.NewWithAdapter(adapter)
  25. fmt.Println(config.MustGet(ctx, "server.address").String())
  26. fmt.Println(config.MustGet(ctx, "database.default").Map())
  27. }

After running, the terminal outputs:

  1. :8888
  2. map[debug:true link:mysql:root:12345678@tcp(127.0.0.1:3306)/test]