全局对象

GF框架封装了一些常用的数据类型以及对象,可以直接通过g.*方便获取。

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

使用方式

  1. import "github.com/gogf/gf/g"

数据类型

  1. // 泛型
  2. type Var = gvar.Var
  3. // 常用Map类型
  4. type Map = map[string]interface{}
  5. type MapAnyAny = map[interface{}]interface{}
  6. type MapAnyStr = map[interface{}]string
  7. type MapAnyInt = map[interface{}]int
  8. type MapStrAny = map[string]interface{}
  9. type MapStrStr = map[string]string
  10. type MapStrInt = map[string]int
  11. type MapIntAny = map[int]interface{}
  12. type MapIntStr = map[int]string
  13. type MapIntInt = map[int]int
  14. // 常用Slice Map类型
  15. type List = []Map
  16. type ListAnyStr = []map[interface{}]string
  17. type ListAnyInt = []map[interface{}]int
  18. type ListStrAny = []map[string]interface{}
  19. type ListStrStr = []map[string]string
  20. type ListStrInt = []map[string]int
  21. type ListIntAny = []map[int]interface{}
  22. type ListIntStr = []map[int]string
  23. type ListIntInt = []map[int]int
  24. // 常用Slice类型
  25. type Slice = []interface{}
  26. type SliceAny = []interface{}
  27. type SliceStr = []string
  28. type SliceInt = []int
  29. // 常用Slice类型(别名)
  30. type Array = []interface{}
  31. type ArrayAny = []interface{}
  32. type ArrayStr = []string
  33. type ArrayInt = []int

常用对象

  1. (单例) 配置管理对象
    1. func Config(name...string) *gcfg.Config
  2. (单例) 模板引擎对象
    1. func View(name ...string) *gview.View
  3. (单例) WEB Server
    1. func Server(name ...interface{}) *ghttp.Server
  4. (单例) TCP Server
    1. func TcpServer(name ...interface{}) *gtcp.Server
  5. (单例) UDP Server
    1. func UdpServer(name ...interface{}) *gudp.Server
  6. (单例) 数据库ORM对象
    1. func DB(name ...string) *gdb.Db
  7. (单例) Redis客户端对象
    1. func Redis(name ...string) *gredis.Redis

Debug模式

默认情况下,gf框架的各个模块都会开启调试模式,输出一些调试信息,有助于开发者在开发环境中调试定位问题。但是程序正式部署到生产环境之后,这些调试信息往往没有那么必要,并且过多的调试信息也容易在查看重要业务日志的时候影响视觉。因此我们可以通过一些方式关闭掉调试信息。

  1. (推荐) 使用 g.SetDebug(false) 方法设置;
  2. 修改命令行启动参数 - gf.glog.debug=false
  3. 修改指定的环境变量 - GF_GLOG_DEBUG=false