温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

GO语言中接口和接口型函数如何使用

发布时间:2023-03-09 09:50:53 来源:亿速云 阅读:66 作者:iii 栏目:开发技术

这篇文章主要讲解了“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语言中接口和接口型函数如何使用”的内容了,经过本文的学习后,相信大家对GO语言中接口和接口型函数如何使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI