新聞中心
??GOFrame??框架封裝了一些常用的數(shù)據(jù)類(lèi)型以及對(duì)象獲取方法,通過(guò)??g.*??方法獲取。

??g??是一個(gè)強(qiáng)耦合的模塊,目的是為開(kāi)發(fā)者在對(duì)頻繁使用的類(lèi)型/對(duì)象調(diào)用時(shí)提供便利。
使用方式:
import "github.com/gogf/gf/v2/frame/g"
數(shù)據(jù)類(lèi)型
常用數(shù)據(jù)類(lèi)型別名。
type (
Var = gvar.Var // Var is a universal variable interface, like generics.
Ctx = context.Context // Ctx is alias of frequently-used context.Context.
)
type (
Map = map[string]interface{} // Map is alias of frequently-used map type map[string]interface{}.
MapAnyAny = map[interface{}]interface{} // MapAnyAny is alias of frequently-used map type map[interface{}]interface{}.
MapAnyStr = map[interface{}]string // MapAnyStr is alias of frequently-used map type map[interface{}]string.
MapAnyInt = map[interface{}]int // MapAnyInt is alias of frequently-used map type map[interface{}]int.
MapStrAny = map[string]interface{} // MapStrAny is alias of frequently-used map type map[string]interface{}.
MapStrStr = map[string]string // MapStrStr is alias of frequently-used map type map[string]string.
MapStrInt = map[string]int // MapStrInt is alias of frequently-used map type map[string]int.
MapIntAny = map[int]interface{} // MapIntAny is alias of frequently-used map type map[int]interface{}.
MapIntStr = map[int]string // MapIntStr is alias of frequently-used map type map[int]string.
MapIntInt = map[int]int // MapIntInt is alias of frequently-used map type map[int]int.
MapAnyBool = map[interface{}]bool // MapAnyBool is alias of frequently-used map type map[interface{}]bool.
MapStrBool = map[string]bool // MapStrBool is alias of frequently-used map type map[string]bool.
MapIntBool = map[int]bool // MapIntBool is alias of frequently-used map type map[int]bool.
)
type (
List = []Map // List is alias of frequently-used slice type []Map.
ListAnyAny = []MapAnyAny // ListAnyAny is alias of frequently-used slice type []MapAnyAny.
ListAnyStr = []MapAnyStr // ListAnyStr is alias of frequently-used slice type []MapAnyStr.
ListAnyInt = []MapAnyInt // ListAnyInt is alias of frequently-used slice type []MapAnyInt.
ListStrAny = []MapStrAny // ListStrAny is alias of frequently-used slice type []MapStrAny.
ListStrStr = []MapStrStr // ListStrStr is alias of frequently-used slice type []MapStrStr.
ListStrInt = []MapStrInt // ListStrInt is alias of frequently-used slice type []MapStrInt.
ListIntAny = []MapIntAny // ListIntAny is alias of frequently-used slice type []MapIntAny.
ListIntStr = []MapIntStr // ListIntStr is alias of frequently-used slice type []MapIntStr.
ListIntInt = []MapIntInt // ListIntInt is alias of frequently-used slice type []MapIntInt.
ListAnyBool = []MapAnyBool // ListAnyBool is alias of frequently-used slice type []MapAnyBool.
ListStrBool = []MapStrBool // ListStrBool is alias of frequently-used slice type []MapStrBool.
ListIntBool = []MapIntBool // ListIntBool is alias of frequently-used slice type []MapIntBool.
)
type (
Slice = []interface{} // Slice is alias of frequently-used slice type []interface{}.
SliceAny = []interface{} // SliceAny is alias of frequently-used slice type []interface{}.
SliceStr = []string // SliceStr is alias of frequently-used slice type []string.
SliceInt = []int // SliceInt is alias of frequently-used slice type []int.
)
type (
Array = []interface{} // Array is alias of frequently-used slice type []interface{}.
ArrayAny = []interface{} // ArrayAny is alias of frequently-used slice type []interface{}.
ArrayStr = []string // ArrayStr is alias of frequently-used slice type []string.
ArrayInt = []int // ArrayInt is alias of frequently-used slice type []int.
)
常用對(duì)象
常用對(duì)象往往通過(guò)單例模式進(jìn)行管理,可以根據(jù)不同的單例名稱(chēng)獲取對(duì)應(yīng)的對(duì)象實(shí)例,并在對(duì)象初始化時(shí)會(huì)自動(dòng)檢索獲取配置文件中的對(duì)應(yīng)配置項(xiàng)。
注意事項(xiàng):在運(yùn)行時(shí)階段,每一次通過(guò)?g?模塊獲取單例對(duì)象時(shí)都會(huì)有內(nèi)部全局鎖機(jī)制來(lái)保證操作和數(shù)據(jù)的并發(fā)安全性,原理性上來(lái)講在并發(fā)量大的場(chǎng)景下會(huì)存在鎖競(jìng)爭(zhēng)的情況,但絕大部分的業(yè)務(wù)場(chǎng)景下開(kāi)發(fā)者均不需要太在意鎖競(jìng)爭(zhēng)帶來(lái)的性能損耗。此外,開(kāi)發(fā)者也可以通過(guò)將獲取到的單例對(duì)象保存到特定的模塊下的內(nèi)部變量重復(fù)使用,以此避免運(yùn)行時(shí)鎖競(jìng)爭(zhēng)情況。
HTTP客戶(hù)端對(duì)象
func Client() *ghttp.Client創(chuàng)建一個(gè)新的HTTP客戶(hù)端對(duì)象。
Validator校驗(yàn)對(duì)象
func Validator() *gvalid.Validator創(chuàng)建一個(gè)新的數(shù)據(jù)校驗(yàn)對(duì)象。
(單例) 配置管理對(duì)象
func Cfg(name ...string) *gcfg.Config該單例對(duì)象將會(huì)自動(dòng)按照文件后綴?toml/yaml/yml/json/ini/xml?文自動(dòng)檢索配置文件。默認(rèn)情況下會(huì)自動(dòng)檢索配置文件?config.toml/config.yaml/config.yml/config.json/config.ini/config.xml?并緩存,配置文件在外部被修改時(shí)將會(huì)自動(dòng)刷新緩存。
為方便多文件場(chǎng)景下的配置文件調(diào)用,簡(jiǎn)便使用并提高開(kāi)發(fā)效率,單例對(duì)象在創(chuàng)建時(shí)將會(huì)自動(dòng)使用單例名稱(chēng)進(jìn)行文件檢索。例如:?g.Cfg("redis")?獲取到的單例對(duì)象將默認(rèn)會(huì)自動(dòng)檢索?redis.toml/redis.yaml/redis.yml/redis.json/redis.ini/redis.xml?,如果檢索成功那么將該文件加載到內(nèi)存緩存中,下一次將會(huì)直接從內(nèi)存中讀??;當(dāng)該文件不存在時(shí),則使用默認(rèn)的配置文件(?config.toml?)。
(單例) 日志管理對(duì)象
func Log(name ...string) *glog.Logger該單例對(duì)象將會(huì)自動(dòng)讀取默認(rèn)配置文件中的?logger?配置項(xiàng),并只會(huì)初始化一次日志對(duì)象。
(單例) 模板引擎對(duì)象
func View(name ...string) *gview.View該單例對(duì)象將會(huì)自動(dòng)讀取默認(rèn)配置文件中的?viewer?配置項(xiàng),并只會(huì)初始化一次模板引擎對(duì)象。內(nèi)部采用了懶初始化設(shè)計(jì),獲取模板引擎對(duì)象時(shí)只是創(chuàng)建了一個(gè)輕量的模板管理對(duì)象,只有當(dāng)解析模板文件時(shí)才會(huì)真正初始化。
(單例) WEB Server
func Server(name ...interface{}) *ghttp.Server該單例對(duì)象將會(huì)自動(dòng)讀取默認(rèn)配置文件中的?server?配置項(xiàng),并只會(huì)初始化一次?Server?對(duì)象。
(單例) TCP Server
func TcpServer(name ...interface{}) *gtcp.Server
(單例) UDP Server
func UdpServer(name ...interface{}) *gudp.Server
(單例) 數(shù)據(jù)庫(kù)ORM對(duì)象
func DB(name ...string) *gdb.Db該單例對(duì)象將會(huì)自動(dòng)讀取默認(rèn)配置文件中的?database?配置項(xiàng),并只會(huì)初始化一次?DB?對(duì)象。
此外,可以通過(guò)以下方法在默認(rèn)數(shù)據(jù)庫(kù)上創(chuàng)建一個(gè)?Model?對(duì)象:
func Model(tables string, db ...string) *gdb.Model
(單例) Redis客戶(hù)端對(duì)象
func Redis(name ...string) *gredis.Redis該單例對(duì)象將會(huì)自動(dòng)讀取默認(rèn)配置文件中的?redis?配置項(xiàng),并只會(huì)初始化一次?Redis?對(duì)象。
(單例) 資源管理對(duì)象
func Res(name ...string) *gres.Resource
(單例) 國(guó)際化管理對(duì)象
func I18n(name ...string) *gi18n.Manager 網(wǎng)頁(yè)標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame核心組件-對(duì)象管理
文章鏈接:http://m.fisionsoft.com.cn/article/cososic.html


咨詢(xún)
建站咨詢(xún)
