github.com/gogf/gf@v1.16.9/frame/gins/gins.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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 7 // Package gins provides instances and core components management. 8 package gins 9 10 import ( 11 "github.com/gogf/gf/container/gmap" 12 ) 13 14 var ( 15 // instances is the instance map for common used components. 16 instances = gmap.NewStrAnyMap(true) 17 ) 18 19 // Get returns the instance by given name. 20 func Get(name string) interface{} { 21 return instances.Get(name) 22 } 23 24 // Set sets a instance object to the instance manager with given name. 25 func Set(name string, instance interface{}) { 26 instances.Set(name, instance) 27 } 28 29 // GetOrSet returns the instance by name, 30 // or set instance to the instance manager if it does not exist and returns this instance. 31 func GetOrSet(name string, instance interface{}) interface{} { 32 return instances.GetOrSet(name, instance) 33 } 34 35 // GetOrSetFunc returns the instance by name, 36 // or sets instance with returned value of callback function <f> if it does not exist 37 // and then returns this instance. 38 func GetOrSetFunc(name string, f func() interface{}) interface{} { 39 return instances.GetOrSetFunc(name, f) 40 } 41 42 // GetOrSetFuncLock returns the instance by name, 43 // or sets instance with returned value of callback function <f> if it does not exist 44 // and then returns this instance. 45 // 46 // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f> 47 // with mutex.Lock of the hash map. 48 func GetOrSetFuncLock(name string, f func() interface{}) interface{} { 49 return instances.GetOrSetFuncLock(name, f) 50 } 51 52 // SetIfNotExist sets <instance> to the map if the <name> does not exist, then returns true. 53 // It returns false if <name> exists, and <instance> would be ignored. 54 func SetIfNotExist(name string, instance interface{}) bool { 55 return instances.SetIfNotExist(name, instance) 56 }