Configuration - Methods - 图1tip

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:
  1. GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
  1. func ExampleConfig_GetWithEnv() {
  2. var (
  3. key = `env.test`
  4. ctx = gctx.New()
  5. )
  6. v, err := g.Cfg().GetWithEnv(ctx, key)
  7. if err != nil {
  8. panic(err)
  9. }
  10. fmt.Printf("env:%s\n", v)
  11. if err = genv.Set(`ENV_TEST`, "gf"); err != nil {
  12. panic(err)
  13. }
  14. v, err = g.Cfg().GetWithEnv(ctx, key)
  15. if err != nil {
  16. panic(err)
  17. }
  18. fmt.Printf("env:%s", v)
  19. // Output:
  20. // env:
  21. // env:gf
  22. }

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:
  1. GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
  1. func ExampleConfig_GetWithCmd() {
  2. var (
  3. key = `cmd.test`
  4. ctx = gctx.New()
  5. )
  6. v, err := g.Cfg().GetWithCmd(ctx, key)
  7. if err != nil {
  8. panic(err)
  9. }
  10. fmt.Printf("cmd:%s\n", v)
  11. // Re-Initialize custom command arguments.
  12. os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
  13. gcmd.Init(os.Args...)
  14. // Retrieve the configuration and command option again.
  15. v, err = g.Cfg().GetWithCmd(ctx, key)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Printf("cmd:%s", v)
  20. // Output:
  21. // cmd:
  22. // cmd:yes
  23. }

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:
  1. MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
  1. func ExampleConfig_MustGetWithCmd() {
  2. var (
  3. key = `cmd.test`
  4. ctx = gctx.New()
  5. )
  6. v := g.Cfg().MustGetWithCmd(ctx, key)
  7. fmt.Printf("cmd:%s\n", v)
  8. // Re-Initialize custom command arguments.
  9. os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
  10. gcmd.Init(os.Args...)
  11. // Retrieve the configuration and command option again.
  12. v = g.Cfg().MustGetWithCmd(ctx, key)
  13. fmt.Printf("cmd:%s", v)
  14. // Output:
  15. // cmd:
  16. // cmd:yes
  17. }

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:
  1. MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
  1. func ExampleConfig_MustGetWithEnv() {
  2. var (
  3. key = `env.test`
  4. ctx = gctx.New()
  5. )
  6. v := g.Cfg().MustGetWithEnv(ctx, key)
  7. fmt.Printf("env:%s\n", v)
  8. if err := genv.Set(`ENV_TEST`, "gf"); err != nil {
  9. panic(err)
  10. }
  11. v = g.Cfg().MustGetWithEnv(ctx, key)
  12. fmt.Printf("env:%s", v)
  13. // Output:
  14. // env:
  15. // env:gf
  16. }

Data

  • Description: The Data method obtains configuration data from the configuration object and assembles it into the map[string]interface{} type.
  • Format:
  1. Data(ctx context.Context) (data map[string]interface{}, err error)
  • Example:
  1. func ExampleConfig_Data() {
  2. ctx := gctx.New()
  3. content := `
  4. v1 = 1
  5. v2 = "true"
  6. v3 = "off"
  7. v4 = "1.23"
  8. array = [1,2,3]
  9. [redis]
  10. disk = "127.0.0.1:6379,0"
  11. cache = "127.0.0.1:6379,1"
  12. `
  13. c, err := gcfg.New()
  14. if err != nil{
  15. panic(err)
  16. }
  17. c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
  18. data, err := c.Data(ctx)
  19. if err != nil{
  20. panic(err)
  21. }
  22. fmt.Println(data)
  23. // Output:
  24. // 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]
  25. }

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:
  1. MustData(ctx context.Context) map[string]interface{}
  • Example:
  1. func ExampleConfig_MustData() {
  2. ctx := gctx.New()
  3. content := `
  4. v1 = 1
  5. v2 = "true"
  6. v3 = "off"
  7. v4 = "1.23"
  8. array = [1,2,3]
  9. [redis]
  10. disk = "127.0.0.1:6379,0"
  11. cache = "127.0.0.1:6379,1"
  12. `
  13. c, err := gcfg.New()
  14. if err != nil{
  15. panic(err)
  16. }
  17. c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
  18. data := c.MustData(ctx)
  19. fmt.Println(data)
  20. // Output:
  21. // 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]
  22. }

Get

  • Description: The Get method obtains configuration data from the configuration object, returning a gvar generic object.
  • Format:
  1. Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
  • Example:
  1. func ExampleConfig_Get() {
  2. ctx := gctx.New()
  3. content := `
  4. v1 = 1
  5. v2 = "true"
  6. v3 = "off"
  7. v4 = "1.23"
  8. array = [1,2,3]
  9. [redis]
  10. disk = "127.0.0.1:6379,0"
  11. cache = "127.0.0.1:6379,1"
  12. `
  13. c, err := gcfg.New()
  14. if err != nil{
  15. panic(err)
  16. }
  17. c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
  18. data,err := c.Get(ctx,"redis")
  19. if err != nil {
  20. panic(err)
  21. }
  22. fmt.Println(data)
  23. // Output:
  24. // {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
  25. }

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:
  1. MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
  • Example:
  1. func ExampleConfig_MustGet() {
  2. ctx := gctx.New()
  3. content := `
  4. v1 = 1
  5. v2 = "true"
  6. v3 = "off"
  7. v4 = "1.23"
  8. array = [1,2,3]
  9. [redis]
  10. disk = "127.0.0.1:6379,0"
  11. cache = "127.0.0.1:6379,1"
  12. `
  13. c, err := gcfg.New()
  14. if err != nil{
  15. panic(err)
  16. }
  17. c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
  18. data := c.MustGet(ctx,"redis")
  19. fmt.Println(data)
  20. // Output:
  21. // {"cache":"127.0.0.1:6379,1","disk":"127.0.0.1:6379,0"}
  22. }

GetAdapter

  • Description: The GetAdapter method retrieves the current running gcfg adapter information. For more about adapters, you can click here Configuration - Interface
  • Format:
  1. GetAdapter() Adapter
  • Example:
  1. func ExampleConfig_GetAdapter() {
  2. c, err := gcfg.New()
  3. if err != nil{
  4. panic(err)
  5. }
  6. adapter := c.GetAdapter()
  7. fmt.Println(adapter)
  8. // Output:
  9. // &{config.toml 0xc00014d720 0xc000371880 false}
  10. }

SetAdapter

  • Description: The SetAdapter method sets the current running gcfg adapter information. For more about adapters, you can click here Configuration - Interface
  • Format:
  1. SetAdapter(adapter Adapter)
  • Example:
  1. func ExampleConfig_SetAdapter() {
  2. c, err := gcfg.New()
  3. if err != nil{
  4. panic(err)
  5. }
  6. adapter := c.GetAdapter()
  7. c.SetAdapter(adapter)
  8. fmt.Println(adapter)
  9. // Output:
  10. // &{config.toml 0xc00014d720 0xc000371880 false}
  11. }