【问题标题】:How to tell if an type's base function has been overridden in Go, without calling the function?如何在不调用函数的情况下判断类型的基函数是否已在 Go 中被覆盖?
【发布时间】:2019-03-17 05:04:38
【问题描述】:

我正在用 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())
}

这个 sn-p 将打印出以下内容:

/myEndpoint
myEndpoint get
base post

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

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

我已经使用了反射包,但没有发现任何有用的东西。

【问题讨论】:

  • Override 是编译阶段的一个动作,所以你可以在你的代码中找到它。为什么要在运行时确定它?

标签: function go methods overriding


【解决方案1】:

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

如果你想在运行时检查这个,你可以比较函数指针。您无法比较函数值,它们不可比较(仅与 nil 值)。 Spec: Comparison operators:

切片、映射和函数值不可比较。但是,作为一种特殊情况,切片、映射或函数值可能会与预先声明的标识符 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)

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

882848 882848 true
882880 882912 false

输出告诉Post() 没有被“覆盖”(myEndpointInstance.PostmyEndpointInstance.BaseRoute.Post 相同),而Get() 是(myEndpointInstance.GetmyEndpointInstance.BaseRoute.Get 不同)。

查看相关问题:

How to compare 2 functions in Go?

Collection of Unique Functions in Go

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2018-10-20
    相关资源
    最近更新 更多