欢迎访问WDPHP素材源码!今天是2024年04月29日 星期一,下午好!别打盹哦!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

GO语言中接口和接口型函数如何使用
栏目分类:其他教程    发布日期:2023-06-27    浏览次数:370次     收藏

这篇文章主要讲解了“GO语言中接口和接口型函数如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“GO语言中接口和接口型函数如何使用”吧!

// 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)
}

GO语言中的接口怎么用?

以上的例程中,首先定义了一个接口,随后定义了一个函数类型实现了此接口,那么GO语言中的接口到底怎么使用呢?

在GO语言中,接口是一种类型,它定义了一组方法的组合,但没有具体的代码实现,接口定义示例如下:

type MyInterface interface {
    Method1() string
    Method2(int) int
}

GO语言中,接口的实现是隐式的。接口实现要绑定在一个类型上面,通过实现类型的方法,来隐式的实现接口,实现示例如下:

type MyType struct {
    // type fields
}

func (t *MyType) Method1() string {
    return "Hello, world!"
}

func (t *MyType) Method2(n int) int {
    return n * n
}

实现接口后,我们可以把接口作为参数传入某个函数中,这样实现了接口的不同数据结构就可以都作为接口传入函数中了:

func MyFunction(i MyInterface) {
    fmt.Println(i.Method1())
    fmt.Println(i.Method2(8))
}

调用此函数时,就可以先声明实现了此接口的数据结构,然后调用函数即可:

func main() {
   t := &MyType{}
   MyFunction(t)
}

调用后即可产生结果:

Hello, world!
64

使用函数类型实现接口有何好处?

以上是使用的结构体隐式实现了接口,还可以自定义函数类型来隐式实现接口。这样可以使用匿名函数或者普通函数(都需要类型转换)直接作为接口参数传入函数中。接口及实现如下:

type MyInterface interface {
    Method1() string
}

type MyInterfaceFunc func()string

func (f MyInterfaceFunc) Method1()string {
    return f()
}

定义一个以接口为参数的函数:

func MyFunction(i MyInterfaceFunc){
   fmt.Println(i.Method1())
}

使用普通函数进行调用:

func Dog()string{
   return "dog dog !"
}

func main() {
   MyFunction(MyInterfaceFunc(Dog))
}

使用匿名函数进行调用:

func main() {
   MyFunction(MyInterfaceFunc(func() string {
      return "hello!"
   }))
}

可以看到,最终的输出都是正确的:

dog dog !
hello!

总的来说,大大的增加了代码的可扩展性,如果没有接口型函数的话,那么想要实现一个新的函数,就需要声明新的类型(如结构体),再隐式的实现接口,再传入MyFunction函数中。有了接口型函数后,那么只需要实现核心逻辑,随后将函数转换为预期的类型即可直接传入。

GO源码例子

net/http 的 Handler 和 HandlerFunc 就是一个典型的接口型函数的例子,Handler的定义如下:

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

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

可以使用http.Handle来对经典的映射路径和处理函数进行映射:

func Handle(pattern string, handler Handler)

观察这个函数,可以发现跟上文的例子很类似,这里的handler就是接口类型,然后在HandlerFunc的基础上做了实现,那么我们可以进行如下的使用:

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.ListenAndServe("localhost:8000", nil)
}

运行起来后,就会监听localhost:8000并运行home函数。这里将home进行类型转换为http.HandlerFunc再作为接口类型传入http.Handle,非常的方便。

我们同样可以使用匿名函数的形式:

func main() {
   http.Handle("/home", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
      writer.WriteHeader(http.StatusOK)
      writer.Write([]byte("hello word!"))
   }))
   _ = http.ListenAndServe("localhost:8000", nil)
}

可以达到同样的效果。

相关热词: GO言中接口函数使用

源码 模板 特效 素材 资源 教程 站长