Go之接口型函数用法

在net/http包中,有一个接口型函数的实现:

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

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

为什么在多路复用器中不能直接根据路由取到视图函数HandlerFunc然后加括号执行呢?

反而还要多此一举实现Handler接口,然后将函数包装后HandlerFunc(f).ServeHTTP(w,r)调用呢。

价值

既能够将普通的函数类型(需类型转换)作为参数,也可以将结构体作为参数,使用更为灵活,可读性也更好,这就是接口型函数的价值。

实例1(net/http)

可以 http.Handle 来映射请求路径和处理函数,Handle 的定义如下:

func Handle(pattern string, handler Handler)

第二个参数是即接口类型 Handler,

func home(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    _, _ = w.Write([]byte("hello, index page"))
}

func main() {
    http.Handle("/home", http.HandlerFunc(home))
    // http.HandlerFunc(home)->HandlerFunc->默认的多路复用器会调用它的ServeHTTP()方法
    _ = http.ListenAndServe("localhost:8000", nil)
}

另外一个函数 http.HandleFunc,HandleFunc 的定义如下:

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

第二个参数是一个普通的函数类型,

func main() {
    http.HandleFunc("/home", home)
    _ = http.ListenAndServe("localhost:8000", nil)
}

两种写法是完全等价的,HandleFunc内部将第二种写法转换为了第一种写法。

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    if handler == nil {
        panic("http: nil handler")
    }
    mux.Handle(pattern, HandlerFunc(handler))
}

http.ListenAndServe 的第二个参数也是接口类型 Handler,使用了标准库 net/http 内置的路由,因此传入的值是 nil。

如果这个地方传入的是一个实现了 Handler 接口的结构体,就可以完全托管所有的 HTTP 请求,后续怎么路由,怎么处理,请求前后增加什么功能,都可以自定义了。慢慢地,就变成了一个功能丰富的 Web 框架了。

实例2(tutu)

// A Getter loads data for a key.
type Getter interface {
    Get(key string) ([]byte, error)
}

// A GetterFunc implements Getter with a function.
type GetterFunc func(key string) ([]byte, error)

// Get implements Getter interface function
func (f GetterFunc) Get(key string) ([]byte, error) {
    return f(key)
}

假设有一个方法:

func GetData(getter Getter, key string) []byte {
    buf, err := getter.Get(key)
    if err == nil {
        return buf
    }
    return nil
}

如何给该方法传参呢?

方式一:GetterFunc 类型的函数作为参数(匿名函数)

GetData(GetterFunc(func(key string) ([]byte, error) {
    return []byte(key), nil
}), "hello")

方式二:普通函数

func test(key string) ([]byte, error) {
    return []byte(key), nil
}

func main() {
    GetData(GetterFunc(test), "hello")
}

将 test 强制类型转换为 GetterFunc,GetterFunc 实现了接口 Getter,是一个合法参数。这种方式适用于逻辑较为简单的场景。

方式三:实现了 Getter 接口的结构体作为参数

type DB struct{ url string}

func (db *DB) Query(sql string, args ...string) string {
    // ...
    return "hello"
}

func (db *DB) Get(key string) ([]byte, error) {
    // ...
    v := db.Query("SELECT NAME FROM TABLE WHEN NAME= ?", key)
    return []byte(v), nil
}

func main() {
    GetData(new(DB), "hello")
}

DB 实现了接口 Getter,也是一个合法参数。这种方式适用于逻辑较为复杂的场景,如果对数据库的操作需要很多信息,地址、用户名、密码,还有很多中间状态需要保持,比如超时、重连、加锁等等。这种情况下,更适合封装为一个结构体作为参数。

这样,既能够将普通的函数类型(需类型转换)作为参数,也可以将结构体作为参数,使用更为灵活,可读性也更好,这就是接口型函数的价值。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持aitechtogether.com。

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年4月28日
下一篇 2023年4月28日

相关推荐