小编Wor*_*zza的帖子

在不调用函数的情况下,如何判断类型的基本函数是否已在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 …
Run Code Online (Sandbox Code Playgroud)

methods overriding function go

4
推荐指数
1
解决办法
91
查看次数

标签 统计

function ×1

go ×1

methods ×1

overriding ×1