gres_instance.go 777 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2017 gf 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 gres
  7. import "github.com/gogf/gf/container/gmap"
  8. const (
  9. // Default group name for instance usage.
  10. DefaultName = "default"
  11. )
  12. var (
  13. // Instances map.
  14. instances = gmap.NewStrAnyMap(true)
  15. )
  16. // Instance returns an instance of Resource.
  17. // The parameter <name> is the name for the instance.
  18. func Instance(name ...string) *Resource {
  19. key := DefaultName
  20. if len(name) > 0 && name[0] != "" {
  21. key = name[0]
  22. }
  23. return instances.GetOrSetFuncLock(key, func() interface{} {
  24. return New()
  25. }).(*Resource)
  26. }