g是一个强耦合的模块,目的是为开发者在对频繁使用的类型/对象调用时提供便利。

使用方式

数据类型

常用数据类型别名。

  1. type (
  2. Var = gvar.Var // Var is a universal variable interface, like generics.
  3. Ctx = context.Context // Ctx is alias of frequently-used context.Context.
  4. )
  5. type (
  6. Map = map[string]interface{} // Map is alias of frequently-used map type map[string]interface{}.
  7. MapAnyAny = map[interface{}]interface{} // MapAnyAny is alias of frequently-used map type map[interface{}]interface{}.
  8. MapAnyStr = map[interface{}]string // MapAnyStr is alias of frequently-used map type map[interface{}]string.
  9. MapAnyInt = map[interface{}]int // MapAnyInt is alias of frequently-used map type map[interface{}]int.
  10. MapStrAny = map[string]interface{} // MapStrAny is alias of frequently-used map type map[string]interface{}.
  11. MapStrInt = map[string]int // MapStrInt is alias of frequently-used map type map[string]int.
  12. MapIntAny = map[int]interface{} // MapIntAny is alias of frequently-used map type map[int]interface{}.
  13. MapIntStr = map[int]string // MapIntStr is alias of frequently-used map type map[int]string.
  14. MapIntInt = map[int]int // MapIntInt is alias of frequently-used map type map[int]int.
  15. MapAnyBool = map[interface{}]bool // MapAnyBool is alias of frequently-used map type map[interface{}]bool.
  16. MapStrBool = map[string]bool // MapStrBool is alias of frequently-used map type map[string]bool.
  17. MapIntBool = map[int]bool // MapIntBool is alias of frequently-used map type map[int]bool.
  18. )
  19. type (
  20. ListAnyAny = []MapAnyAny // ListAnyAny is alias of frequently-used slice type []MapAnyAny.
  21. ListAnyStr = []MapAnyStr // ListAnyStr is alias of frequently-used slice type []MapAnyStr.
  22. ListAnyInt = []MapAnyInt // ListAnyInt is alias of frequently-used slice type []MapAnyInt.
  23. ListStrAny = []MapStrAny // ListStrAny is alias of frequently-used slice type []MapStrAny.
  24. ListStrStr = []MapStrStr // ListStrStr is alias of frequently-used slice type []MapStrStr.
  25. ListStrInt = []MapStrInt // ListStrInt is alias of frequently-used slice type []MapStrInt.
  26. ListIntAny = []MapIntAny // ListIntAny is alias of frequently-used slice type []MapIntAny.
  27. ListIntStr = []MapIntStr // ListIntStr is alias of frequently-used slice type []MapIntStr.
  28. ListIntInt = []MapIntInt // ListIntInt is alias of frequently-used slice type []MapIntInt.
  29. ListAnyBool = []MapAnyBool // ListAnyBool is alias of frequently-used slice type []MapAnyBool.
  30. ListStrBool = []MapStrBool // ListStrBool is alias of frequently-used slice type []MapStrBool.
  31. ListIntBool = []MapIntBool // ListIntBool is alias of frequently-used slice type []MapIntBool.
  32. )
  33. type (
  34. Slice = []interface{} // Slice is alias of frequently-used slice type []interface{}.
  35. SliceAny = []interface{} // SliceAny is alias of frequently-used slice type []interface{}.
  36. SliceStr = []string // SliceStr is alias of frequently-used slice type []string.
  37. SliceInt = []int // SliceInt is alias of frequently-used slice type []int.
  38. )
  39. type (
  40. Array = []interface{} // Array is alias of frequently-used slice type []interface{}.
  41. ArrayAny = []interface{} // ArrayAny is alias of frequently-used slice type []interface{}.
  42. ArrayStr = []string // ArrayStr is alias of frequently-used slice type []string.
  43. ArrayInt = []int // ArrayInt is alias of frequently-used slice type []int.
  44. )

常用对象

常用对象往往通过单例模式进行管理,可以根据不同的单例名称获取对应的对象实例,并在对象初始化时会自动检索获取配置文件中的对应配置项,具体配置项请查看对应对象的章节介绍。

  1. func Client() *ghttp.Client

创建一个新的客户端对象。

Validator校验对象

  1. func Validator() *gvalid.Validator

创建一个新的数据校验对象。

(单例) 配置管理对象

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

该单例对象将会自动按照文件后缀toml/yaml/yml/json/ini/xml文自动检索配置文件。默认情况下会自动检索配置文件config.toml/config.yaml/config.yml/config.json/config.ini/config.xml并缓存,配置文件在外部被修改时将会自动刷新缓存。

为方便多文件场景下的配置文件调用,简便使用并提高开发效率,单例对象在创建时将会自动使用单例名称进行文件检索。例如:g.Cfg("redis")获取到的单例对象将默认会自动检索redis.toml/redis.yaml/redis.yml/redis.json/redis.ini/redis.xml,如果检索成功那么将该文件加载到内存缓存中,下一次将会直接从内存中读取;当该文件不存在时,则使用默认的配置文件(config.toml)。

(单例) 日志管理对象

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

该单例对象将会自动读取默认配置文件中的viewer配置项,并只会初始化一次模板引擎对象。内部采用了懒初始化设计,获取模板引擎对象时只是创建了一个轻量的模板管理对象,只有当解析模板文件时才会真正初始化。

(单例) WEB Server

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

该单例对象将会自动读取默认配置文件中的server配置项,并只会初始化一次Server对象。

(单例) TCP Server

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

(单例) UDP Server

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

该单例对象将会自动读取默认配置文件中的database配置项,并只会初始化一次DB对象。

此外,可以通过以下方法在默认数据库上创建一个Model对象:

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

(单例) Redis客户端对象

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

(单例) 资源管理对象

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

(单例) 国际化管理对象

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