gcfg_instance.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gcfg
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/container/gmap"
  10. )
  11. const (
  12. // Default group name for instance usage.
  13. DefaultName = "default"
  14. )
  15. var (
  16. // Instances map containing configuration instances.
  17. instances = gmap.NewStrAnyMap(true)
  18. )
  19. // Instance returns an instance of Config with default settings.
  20. // The parameter <name> is the name for the instance. But very note that, if the file "name.toml"
  21. // exists in the configuration directory, it then sets it as the default configuration file. The
  22. // toml file type is the default configuration file type.
  23. func Instance(name ...string) *Config {
  24. key := DefaultName
  25. if len(name) > 0 && name[0] != "" {
  26. key = name[0]
  27. }
  28. return instances.GetOrSetFuncLock(key, func() interface{} {
  29. c := New()
  30. for _, fileType := range supportedFileTypes {
  31. if file := fmt.Sprintf(`%s.%s`, key, fileType); c.Available(file) {
  32. c.SetFileName(file)
  33. break
  34. }
  35. }
  36. return c
  37. }).(*Config)
  38. }