【发布时间】: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