r := gin.Default()
Default() function returns *Engine. but r calls a function on RouterGroup.
ex)
r.GET("/api/healthcheck", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hi",
})
})
Definition
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default()
type Engine struct {
RouterGroup
// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
Handlers HandlersChain
basePath string
engine *Engine
root bool
}
Can you explain how this works? what syntax is it
A field without an explicit name is called an "embedded field". The fields and methods of an embedded field's type are "promoted" to the embedding (or "parent") struct. The "promoted" fields and methods of an embedded field's type are directly accessible to the embedding (or "parent") struct. Note that "directly accessible" does not mean "inheritance", it just means that you don't have to specify the embedded field's name in the selector expression. So, r.RouterGroup.GET
and r.GET
are identical.