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:
viewpath: "/home/www/templates/"
database:
default:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
role: "master"
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
role: "slave"
Code example:
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var ctx = gctx.New()
fmt.Println(g.Cfg().Get(ctx, "viewpath"))
fmt.Println(g.Cfg().Get(ctx, "database.default.0.role"))
}
The above example reads the role
information of the first database configuration. After running, the output is:
/home/www/templates/
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.
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var ctx = gctx.New()
fmt.Println(gcfg.Instance().Get(ctx, "viewpath"))
fmt.Println(gcfg.Instance().Get(ctx, "database.default.0.role"))
}
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.