We recommend using the singleton pattern to obtain the configuration management object. We can conveniently get the default global configuration management object through g.Cfg(). Additionally, we can get the configuration management object singleton through the gcfg.Instance package method.

Using g.Cfg

Let’s look at an example demonstrating how to read global configuration information. It’s important to note that the global configuration is framework-related, so it is consistently obtained using g.Cfg(). Below is a default global configuration file, which includes the directory configuration for the template engine and the configuration for a MySQL database cluster (two master servers).

Configuration example:

  1. viewpath: "/home/www/templates/"
  2. database:
  3. default:
  4. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  5. role: "master"
  6. - link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  7. role: "slave"

Code example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func main() {
  8. var ctx = gctx.New()
  9. fmt.Println(g.Cfg().Get(ctx, "viewpath"))
  10. fmt.Println(g.Cfg().Get(ctx, "database.default.0.role"))
  11. }

The above example reads the role information of the first database configuration. After running, the output is:

  1. /home/www/templates/
  2. master

As you can see, we can obtain a global configuration manager singleton object using the g.Cfg() method. The configuration file contents can be accessed hierarchically using a . (dot) in the pattern (arrays start at 0 by default). The pattern parameter database.default.0.role refers to reading the role data of the 0th database server in the default database cluster within the database configuration item.

Using gcfg.Instance

Of course, you can also independently use the gcfg package to get a singleton object through the Instance method.

  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. func main() {
  8. var ctx = gctx.New()
  9. fmt.Println(gcfg.Instance().Get(ctx, "viewpath"))
  10. fmt.Println(gcfg.Instance().Get(ctx, "database.default.0.role"))
  11. }

Auto Search Feature

When a singleton object is created, it will automatically search for configuration files based on file extensions toml/yaml/yml/json/ini/xml/properties. By default, it will automatically search and cache configuration files like config.toml/yaml/yml/json/ini/xml/properties. When a configuration file is modified externally, the cache will automatically refresh.

To facilitate calls to configuration files in multi-file scenarios, simplify usage, and enhance development efficiency, the singleton object will automatically use the singleton name for file searching upon creation. For instance, the singleton object obtained with g.Cfg("redis") will automatically search for redis.toml/yaml/yml/json/ini/xml/properties. If the search is successful, the file will be loaded into memory cache, and the next time, it will be read directly from memory. If the file does not exist, the default configuration file (config.toml) is used.