Skip to main content
Version: 2.8.x(Latest)
tip

The following common method list may lag behind code updates in the documentation. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/gcfg

GetWithEnv

  • Description
    • The GetWithEnv method first obtains configuration data from the default configuration file. If the result is empty, it will try to get it from the current environment variables. Note the naming conversion rules:
    • Environment variables convert the name to uppercase, and the . character in the name is converted to the _ character.
    • Parameter names convert the name to lowercase, and the _ character in the name is converted to the . character.
  • Format:
GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
func ExampleConfig_GetWithEnv() {
var (
key = `env.test`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithEnv(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("env:%s\n", v)
if err = genv.Set(`ENV_TEST`, "gf"); err != nil {
panic(err)
}
v, err = g.Cfg().GetWithEnv(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("env:%s", v)

// Output:
// env:
// env:gf
}

GetWithCmd

  • Description: The GetWithCmd method is similar to the GetWithEnv method. It first obtains configuration data from the default configuration object, but if the data is empty, it obtains configuration information from the command line.
  • Format:
GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
func ExampleConfig_GetWithCmd() {
var (
key = `cmd.test`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("cmd:%s\n", v)
// Re-Initialize custom command arguments.
os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
gcmd.Init(os.Args...)
// Retrieve the configuration and command option again.
v, err = g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("cmd:%s", v)

// Output:
// cmd:
// cmd:yes
}

MustGetWithCmd

  • Description: The MustGetWithCmd method is similar to the GetWithCmd method. It only returns configuration content; if any error occurs internally, a panic will occur.
  • Format:
MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
func ExampleConfig_MustGetWithCmd() {
var (
key = `cmd.test`
ctx = gctx.New()
)
v := g.Cfg().MustGetWithCmd(ctx, key)

fmt.Printf("cmd:%s\n", v)
// Re-Initialize custom command arguments.
os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
gcmd.Init(os.Args...)
// Retrieve the configuration and command option again.
v = g.Cfg().MustGetWithCmd(ctx, key)

fmt.Printf("cmd:%s", v)

// Output:
// cmd:
// cmd:yes
}

MustGetWithEnv

  • Description: The MustGetWithEnv method is similar to the GetWithEnv method. It only returns configuration content; if any error occurs internally, a panic will occur.
  • Format:
MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
func ExampleConfig_MustGetWithEnv() {
var (
key = `env.test`
ctx = gctx.New()
)
v := g.Cfg().MustGetWithEnv(ctx, key)

fmt.Printf("env:%s\n", v)
if err := genv.Set(`ENV_TEST`, "gf"); err != nil {
panic(err)
}
v = g.Cfg().MustGetWithEnv(ctx, key)

fmt.Printf("env:%s", v)

// Output:
// env:
// env:gf
}

Data

  • Description: The Data method obtains configuration data from the configuration object and assembles it into the map[string]interface{} type.
  • Format:
Data(ctx context.Context) (data map[string]interface{}, err error)
  • Example:
func ExampleConfig_Data() {
ctx := gctx.New()
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
c, err := gcfg.New()
if err != nil{
panic(err)
}
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
data, err := c.Data(ctx)
if err != nil{
panic(err)
}

fmt.Println(data)

// Output:
// map[array:[1 2 3] redis:map[cache:127.0.0.1:6379,1 disk:127.0.0.1:6379,0] v1:1 v2:true v3:off v4:1.23]
}

MustData

  • Description: The MustData method obtains configuration data from the configuration object and assembles it into the map[string]interface{} type. It does not return an error if it encounters one internally, but instead directly panics.
  • Format:
MustData(ctx context.Context) map[string]interface{}
  • Example:
func ExampleConfig_MustData() {
ctx := gctx.New()
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
c, err := gcfg.New()
if err != nil{
panic(err)
}

c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
data := c.MustData(ctx)

fmt.Println(data)

// Output:
// map[array:[1 2 3] redis:map[cache:127.0.0.1:6379,1 disk:127.0.0.1:6379,0] v1:1 v2:true v3:off v4:1.23]
}

Get

  • Description: The Get method obtains configuration data from the configuration object, returning a gvar generic object.
  • Format:
Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
func ExampleConfig_Get() {
ctx := gctx.New()
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
c, err := gcfg.New()
if err != nil{
panic(err)
}

c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
data,err := c.Get(ctx,"redis")

if err != nil {
panic(err)
}
fmt.Println(data)

// Output:
// {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
}

MustGet

  • Description: The MustGet method is similar to Get; it also obtains configuration data from the configuration object, assembling it into a gvar structure. It only returns one parameter: *gvar.Var.
  • Note: If the configuration file does not exist or if there’s another error, it will directly panic; proper exception handling is needed.
  • Format:
MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
func ExampleConfig_MustGet() {
ctx := gctx.New()
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
c, err := gcfg.New()
if err != nil{
panic(err)
}

c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
data := c.MustGet(ctx,"redis")

fmt.Println(data)

// Output:
// {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
}

GetAdapter

  • Description: The GetAdapter method retrieves the current running gcfg adapter information. For more about adapters, you can click here Configuration - Interface
  • Format:
GetAdapter() Adapter
  • Example:
func ExampleConfig_GetAdapter() {
c, err := gcfg.New()
if err != nil{
panic(err)
}

adapter := c.GetAdapter()
fmt.Println(adapter)

// Output:
// &{config.toml 0xc00014d720 0xc000371880 false}
}

SetAdapter

  • Description: The SetAdapter method sets the current running gcfg adapter information. For more about adapters, you can click here Configuration - Interface
  • Format:
SetAdapter(adapter Adapter)
  • Example:
func ExampleConfig_SetAdapter() {
c, err := gcfg.New()
if err != nil{
panic(err)
}

adapter := c.GetAdapter()
c.SetAdapter(adapter)
fmt.Println(adapter)

// Output:
// &{config.toml 0xc00014d720 0xc000371880 false}
}