The gcfg component adopts an interface design to achieve high scalability. Through interface design, users can customize configuration retrieval methods, such as etcd, zookeeper, consul, kubernetes configmap, apollo, etc.

Interface Definition

https://github.com/gogf/gf/blob/master/os/gcfg/gcfg_adaper.go

  1. // Adapter is the interface for configuration retrieving.
  2. type Adapter interface {
  3. // Available checks and returns the configuration service is available.
  4. // The optional parameter `resource` specifies certain configuration resource.
  5. //
  6. // It returns true if configuration file is present in default AdapterFile, or else false.
  7. // Note that this function does not return error as it just does simply check for backend configuration service.
  8. Available(ctx context.Context, resource ...string) (ok bool)
  9. // Get retrieves and returns value by specified `pattern`.
  10. Get(ctx context.Context, pattern string) (value interface{}, err error)
  11. // Data retrieves and returns all configuration data as map type.
  12. // Note that this function may lead lots of memory usage if configuration data is too large,
  13. // you can implement this function if necessary.
  14. Data(ctx context.Context) (data map[string]interface{}, err error)
  15. }

Setting Interface Implementation

The configuration object can set the currently used interface implementation through the SetAdapter method.

  1. // SetAdapter sets the adapter of current Config object.
  2. func (c *Config) SetAdapter(adapter Adapter)

Retrieving Interface Implementation

The configuration object can retrieve the currently used interface implementation through the GetAdapter method.

  1. // GetAdapter returns the adapter of current Config object.
  2. func (c *Config) GetAdapter() Adapter

Documentation

📄️ Configuration - AdapterFileIn the GoFrame framework, configuration management is implemented primarily through AdapterFile for file-based configuration loading and reading. Users can conveniently use configuration management through the g.Cfg singleton object or create configuration management objects via the gcfg.NewWithAdapter method. Example code demonstrates how to implement and execute these configuration operations in Golang.

📄️ Configuration - AdapterContentUse the AdapterContent interface in the GoFrame framework to manage configurations. Users can generate the corresponding Adapter interface object by providing specific configuration content, supporting multiple formats. Example code demonstrates how to use the g.Cfg singleton for file-based configuration management.