功能实现界面 [英] Function implementing interface

查看:43
本文介绍了功能实现界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这里发生了什么.

I want to know what is happening here.

有一个http处理程序的接口:

There is the interface for a http handler:

type Handler interface {
    ServeHTTP(*Conn, *Request)
}

我认为我了解这种实现方式.

This implementation I think I understand.

type Counter int

func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
    fmt.Fprintf(c, "counter = %d\n", ctr);
    ctr++;
}

据我了解,类型"Counter"实现了接口,因为它具有一种具有所需签名的方法.到现在为止还挺好.然后给出这个例子:

From my understanding it is that the type "Counter" implements the interface since it has a method that has the required signature. So far so good. Then this example is given:

func notFound(c *Conn, req *Request) {
    c.SetHeader("Content-Type", "text/plain;", "charset=utf-8");
    c.WriteHeader(StatusNotFound);
    c.WriteString("404 page not found\n");
}

// Now we define a type to implement ServeHTTP:
type HandlerFunc func(*Conn, *Request)
func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
    f(c, req) // the receiver's a func; call it
}
// Convert function to attach method, implement the interface:
var Handle404 = HandlerFunc(notFound);

有人可以详细说明为什么这些功能或如何将它们组合在一起吗?

Can somebody elaborate on why or how these various functions fit together?

推荐答案

此:

type Handler interface {
    ServeHTTP(*Conn, *Request)
}

表示任何满足 Handler 接口的类型都必须具有 ServeHTTP 方法.上面的代码位于 http 包中.

says that any type which satisfies the Handler interface must have a ServeHTTP method. The above would be inside the package http.

type Counter int

func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
    fmt.Fprintf(c, "counter = %d\n", ctr);
    ctr++;
}

这会在与ServeHTTP对应的Counter类型上放置一个方法.这是一个与以下示例分开的示例.

This puts a method on the Counter type which corresponds to ServeHTTP. This is an example which is separate from the following.

据我了解,类型"Counter"实现接口,因为它有一种方法具有必需的签名.

From my understanding it is that the type "Counter" implements the interface since it has a method that has the required signature.

是的.

以下功能本身不能用作 Handler :

The following function by itself won't work as a Handler:

func notFound(c *Conn, req *Request) {
    c.SetHeader("Content-Type", "text/plain;", "charset=utf-8");
    c.WriteHeader(StatusNotFound);
    c.WriteString("404 page not found\n");
}

其余的这些内容只适合上面的内容,因此它可以是 Handler .

The rest of this stuff is just fitting the above so that it can be a Handler.

在下面, HandlerFunc 是一个带有两个参数的函数,指向 Conn 的指针和指向的指针请求 ,但不返回任何内容.换句话说,任何使用这些参数且不返回任何值的函数都可以是 HandlerFunc .

In the following, a HandlerFunc is a function which takes two arguments, pointer to Conn and pointer to Request, and returns nothing. In other words, any function which takes these arguments and returns nothing can be a HandlerFunc.

// Now we define a type to implement ServeHTTP:
type HandlerFunc func(*Conn, *Request)

此处 ServeHTTP 是添加到 HandlerFunc 类型的方法:

Here ServeHTTP is a method added to the type HandlerFunc:

func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
    f(c, req) // the receiver's a func; call it
}

它所做的就是用给定的参数调用函数本身( f ).

All it does is to call the function itself (f) with the arguments given.

// Convert function to attach method, implement the interface:
var Handle404 = HandlerFunc(notFound);

在上一行中,通过在函数本身之外人为地创建类型实例并将该函数变为实例的 ServeHTTP 方法.现在, Handle404 可以与 Handler 接口一起使用.这基本上是一种技巧.

In the above line, notFound has been finagled into being acceptable for the interface for Handler by artificially creating a type instance out of the function itself and making the function into the ServeHTTP method for the instance. Now Handle404 can be used with the Handler interface. It's basically a kind of trick.

这篇关于功能实现界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆