Skip to main content
Version: 2.8.x(Latest)

The GoFrame framework encapsulates some commonly used data types and object acquisition methods, which can be obtained through the g.* method.

tip

g is a highly coupled module aimed at providing convenience to developers when frequently using certain types/objects.

Usage:

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

Data Types

Common data type aliases.

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

Common Objects

Common objects are often managed through the singleton pattern, where specific object instances can be obtained according to different singleton names, and corresponding configuration items in the configuration file will be automatically retrieved during object initialization. For specific configuration items, please refer to the respective object's chapter introduction.

info

Note: During runtime, every time a singleton object is obtained through the g module, there will be an internal global lock mechanism to ensure concurrent safety of operations and data. In theory, there may be lock competition in scenarios with high concurrency, but in most business scenarios, developers do not need to overly worry about the performance loss caused by lock competition. Additionally, developers can also save the obtained singleton objects into internal variables of specific modules for reuse, thereby avoiding runtime lock competition situations.

HTTP Client Object

func Client() *ghttp.Client

Creates a new HTTP client object.

Validator Object

func Validator() *gvalid.Validator

Creates a new data validation object.

(Singleton) Configuration Management Object

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

This singleton object will automatically retrieve configuration files based on file extensions toml/yaml/yml/json/ini/xml/properties. By default, it will automatically retrieve the following configuration files:

  • config
  • config.toml
  • config.yaml
  • config.yml
  • config.json
  • config.ini
  • config.xml
  • config.properties

and cache them. The cache will automatically refresh if the configuration files are modified externally.

For convenient calling of configuration files in scenarios with multiple files, to simplify usage and improve development efficiency, the singleton object will automatically use the singleton name for file retrieval at the time of creation. For example: the singleton object retrieved by g.Cfg("redis") will, by default, automatically retrieve the following files:

  • redis
  • redis.toml
  • redis.yaml
  • redis.yml
  • redis.json
  • redis.ini
  • redis.xml
  • redis.properties

If the retrieval is successful, the file will be loaded into memory cache, and next time it will directly read from memory; if the file does not exist, it will use the default configuration file (config.toml).

(Singleton) Log Management Object

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

This singleton object will automatically read the logger configuration item from the default configuration file, and only initialize the log object once.

(Singleton) Template Engine Object

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

This singleton object will automatically read the viewer configuration item from the default configuration file and only initialize the template engine object once. It uses a lazy initialization design internally, creating a lightweight template management object when obtaining the template engine object, and only initializing it when parsing template files.

(Singleton) WEB Server

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

This singleton object will automatically read the server configuration item from the default configuration file and only initialize the Server object once.

(Singleton) TCP Server

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

(Singleton) UDP Server

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

(Singleton) Database ORM Object

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

This singleton object will automatically read the database configuration item from the default configuration file and only initialize the DB object once.

Additionally, the following method can be used to create a Model object on the default database:

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

(Singleton) Redis Client Object

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

This singleton object will automatically read the redis configuration item from the default configuration file and only initialize the Redis object once.

(Singleton) Resource Management Object

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

(Singleton) Internationalization Management Object

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