r := gin.Default() 

默认()函数返回*Engine. 但r调用RouterGroup上的函数.

(前)

    r.GET("/api/healthcheck", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "hi",
        })
    })

定义

// 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
}

你能解释一下这是怎么回事吗?它的语法是什么

推荐答案

没有显式名称的字段称为"嵌入字段".嵌入字段类型的字段和方法被"提升"为嵌入(或"父") struct .嵌入(或"父") struct 可以directly访问嵌入字段类型的"提升"的字段和方法.请注意,"直接可访问"并不意味着"继承",它只是意味着您不必在 Select 器表达式中指定嵌入字段的名称.因此,r.RouterGroup.GETr.GET是相同的.

https://go.dev/ref/spec#Struct_types

Go相关问答推荐

如何从google.golang.org/grpc/stats包中将golang中不同事件的输出进行组合,以获取func HandlePRC

使用恶意软件 scanner (ClamAV)时的Google云存储桶上传文件验证

如何使用GRPC UnaryClientInterceptor中的`maily`参数?

Term~T中的类型不能是类型参数,但可以是引用类型参数的切片

Golang Viper:如果第一个字段不存在,如何从另一个字段获取值

Golang文本/模板序列范围

如何在围棋中从多部分.Part中获取多部分.文件而不保存到磁盘?

正确使用pgtype的方法

调用库和直接操作效率有区别吗?

如何创建在responseWriter.Write 上返回错误的http.ResponseWriter 模拟实例?

在 Go sync.Map 中,为什么这部分实现不一致或者我误解了什么?

Go Template if 条件

使用 Go 根据 /etc/shadow 文件中的散列密码验证密码

Gorm 预加载给出了模糊的列错误

有没有什么方法可以在不使用 if/else 的情况下在 Golang 中处理 nil 指针?

使用 GO 在侧 tar 文件中提取 tar 文件的最快方法

github.com/rs/zerolog 字段的延迟判断

Golang泛型在用作 map 元素时不起作用

Golang - 使用正则表达式提取链接

在 Go 泛型中,如何对联合约束中的类型使用通用方法?