在不调用函数的情况下,如何判断类型的基本函数是否已在Go中被覆盖?

Wor*_*zza 4 methods overriding function go

我正在Go中实现一个简单的路由器。当没有为该终结点实现调用的方法时,我曾经为每个终结点返回一个错误而使用大量冗余代码。我重构并制作了一个“基本”类型,该类型为每种请求类型提供了默认功能,这些功能仅返回未实现的错误。现在,我要做的就是为我要实现的给定端点重写特定的方法功能。在给定端点变量的情况下,直到我想弄清楚哪些方法已被覆盖,这一切都是有趣的游戏。

省略无关的细节,这是我现在想到的一个简单示例:

package main

import (
    "fmt"
)

// Route defines the HTTP method handlers.
type Route interface {
    Get() string
    Post() string
}

// BaseRoute is the "fallback" handlers,
// if those handlers aren't defined later.
type BaseRoute struct{}

func (BaseRoute) Get() string {
    return "base get"
}

func (BaseRoute) Post() string {
    return "base post"
}

// Endpoint holds a route for handling the HTTP request,
// and some other metadata related to that request.
type Endpoint struct {
    BaseRoute
    URI string
}

// myEndpoint is an example endpoint implementation
// which only implements a GET request.
type myEndpoint Endpoint

func (myEndpoint) Get() string {
    return "myEndpoint get"
}

func main() {
    myEndpointInstance := myEndpoint{URI: "/myEndpoint"}
    fmt.Println(myEndpointInstance.URI)
    fmt.Println(myEndpointInstance.Get())
    fmt.Println(myEndpointInstance.Post())
}
Run Code Online (Sandbox Code Playgroud)

该代码段将打印出以下内容:

/myEndpoint
myEndpoint get
base post
Run Code Online (Sandbox Code Playgroud)

因此,我对功能的压倒性按预期工作。现在,我想知道在我的主函数中,声明myEndpointInstance之后,能否以某种方式告诉您Post函数没有被重写,并且仍然由底层BaseRoute实现,而没有实际调用该函数?理想情况下,我想要这样的东西:

func main() {
    myEndpointInstance := myEndpoint{URI: "/myEndpoint"}
    if myEndpointInstace.Post != BaseRoute.Post {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经使用了反射包,但没有发现任何帮助。

icz*_*cza 5

正如其他人所指出的,调用哪种方法是编译时决策。因此,您可以在编译时进行检查,大多数IDE会将您导航到绑定到实际调用的方法。

如果要在运行时进行检查,则可以比较函数指针。您无法比较函数值,它们无法比较(仅与该nil值)。规格:比较运算符

切片,贴图和函数值不可比较。但是,在特殊情况下,可以将切片,映射或函数值与预先声明的标识符进行比较nil

这样可以做到这一点:

myEndpointInstance := myEndpoint{URI: "/myEndpoint"}

v1 := reflect.ValueOf(myEndpointInstance.Post).Pointer()
v2 := reflect.ValueOf(myEndpointInstance.BaseRoute.Post).Pointer()
fmt.Println(v1, v2, v1 == v2)

v1 = reflect.ValueOf(myEndpointInstance.Get).Pointer()
v2 = reflect.ValueOf(myEndpointInstance.BaseRoute.Get).Pointer()
fmt.Println(v1, v2, v1 == v2)
Run Code Online (Sandbox Code Playgroud)

这将输出(在Go Playground上尝试):

882848 882848 true
882880 882912 false
Run Code Online (Sandbox Code Playgroud)

输出表明Post()不是“ overridden”(myEndpointInstance.Post与相同myEndpointInstance.BaseRoute.Post),而Get()is(myEndpointInstance.Get与则不同myEndpointInstance.BaseRoute.Get)。

查看相关问题:

如何在Go中比较2个功能?

Go中独特功能的集合