日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame核心組件-對象管理

??GOFrame??框架封裝了一些常用的數(shù)據(jù)類型以及對象獲取方法,通過??g.*??方法獲取。

??g??是一個強(qiáng)耦合的模塊,目的是為開發(fā)者在對頻繁使用的類型/對象調(diào)用時提供便利。

使用方式:

import "github.com/gogf/gf/v2/frame/g"

數(shù)據(jù)類型

常用數(shù)據(jù)類型別名。

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.
)

常用對象

常用對象往往通過單例模式進(jìn)行管理,可以根據(jù)不同的單例名稱獲取對應(yīng)的對象實(shí)例,并在對象初始化時會自動檢索獲取配置文件中的對應(yīng)配置項(xiàng)。

注意事項(xiàng):在運(yùn)行時階段,每一次通過?g?模塊獲取單例對象時都會有內(nèi)部全局鎖機(jī)制來保證操作和數(shù)據(jù)的并發(fā)安全性,原理性上來講在并發(fā)量大的場景下會存在鎖競爭的情況,但絕大部分的業(yè)務(wù)場景下開發(fā)者均不需要太在意鎖競爭帶來的性能損耗。此外,開發(fā)者也可以通過將獲取到的單例對象保存到特定的模塊下的內(nèi)部變量重復(fù)使用,以此避免運(yùn)行時鎖競爭情況。

HTTP客戶端對象

func Client() *ghttp.Client

創(chuàng)建一個新的HTTP客戶端對象。

Validator校驗(yàn)對象

func Validator() *gvalid.Validator

創(chuàng)建一個新的數(shù)據(jù)校驗(yàn)對象。

(單例) 配置管理對象

func Cfg(name ...string) *gcfg.Config

該單例對象將會自動按照文件后綴?toml/yaml/yml/json/ini/xml?文自動檢索配置文件。默認(rèn)情況下會自動檢索配置文件?config.toml/config.yaml/config.yml/config.json/config.ini/config.xml?并緩存,配置文件在外部被修改時將會自動刷新緩存。

為方便多文件場景下的配置文件調(diào)用,簡便使用并提高開發(fā)效率,單例對象在創(chuàng)建時將會自動使用單例名稱進(jìn)行文件檢索。例如:?g.Cfg("redis")?獲取到的單例對象將默認(rèn)會自動檢索?redis.toml/redis.yaml/redis.yml/redis.json/redis.ini/redis.xml?,如果檢索成功那么將該文件加載到內(nèi)存緩存中,下一次將會直接從內(nèi)存中讀取;當(dāng)該文件不存在時,則使用默認(rèn)的配置文件(?config.toml?)。

(單例) 日志管理對象

func Log(name ...string) *glog.Logger

該單例對象將會自動讀取默認(rèn)配置文件中的?logger?配置項(xiàng),并只會初始化一次日志對象。

(單例) 模板引擎對象

func View(name ...string) *gview.View

該單例對象將會自動讀取默認(rèn)配置文件中的?viewer?配置項(xiàng),并只會初始化一次模板引擎對象。內(nèi)部采用了懶初始化設(shè)計(jì),獲取模板引擎對象時只是創(chuàng)建了一個輕量的模板管理對象,只有當(dāng)解析模板文件時才會真正初始化。

(單例) WEB Server

func Server(name ...interface{}) *ghttp.Server

該單例對象將會自動讀取默認(rèn)配置文件中的?server?配置項(xiàng),并只會初始化一次?Server?對象。

(單例) TCP Server

func TcpServer(name ...interface{}) *gtcp.Server

(單例) UDP Server

func UdpServer(name ...interface{}) *gudp.Server

(單例) 數(shù)據(jù)庫ORM對象

func DB(name ...string) *gdb.Db

該單例對象將會自動讀取默認(rèn)配置文件中的?database?配置項(xiàng),并只會初始化一次?DB?對象。

此外,可以通過以下方法在默認(rèn)數(shù)據(jù)庫上創(chuàng)建一個?Model?對象:

func Model(tables string, db ...string) *gdb.Model

(單例) Redis客戶端對象

func Redis(name ...string) *gredis.Redis

該單例對象將會自動讀取默認(rèn)配置文件中的?redis?配置項(xiàng),并只會初始化一次?Redis?對象。

(單例) 資源管理對象

func Res(name ...string) *gres.Resource

(單例) 國際化管理對象

func I18n(name ...string) *gi18n.Manager

網(wǎng)頁題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame核心組件-對象管理
URL分享:http://www.5511xx.com/article/cososic.html