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.
- The
- 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 theGetWithEnv
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 theGetWithCmd
method. It only returns configuration content; if any error occurs internally, apanic
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 theGetWithEnv
method. It only returns configuration content; if any error occurs internally, apanic
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 themap[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 themap[string]interface{}
type. It does not return an error if it encounters one internally, but instead directlypanic
s. - 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 agvar
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 toGet
; it also obtains configuration data from the configuration object, assembling it into agvar
structure. It only returns one parameter:*gvar.Var
. - Note: If the configuration file does not exist or if there’s another
error
, it will directlypanic
; 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 runninggcfg
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 runninggcfg
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}
}