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

其他教程   发布日期:2023年08月11日   浏览次数:458

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

  1. // A Getter loads data for a key.
  2. type Getter interface {
  3. Get(key string) ([]byte, error)
  4. }
  5. // A GetterFunc implements Getter with a function.
  6. type GetterFunc func(key string) ([]byte, error)
  7. // Get implements Getter interface function
  8. func (f GetterFunc) Get(key string) ([]byte, error) {
  9. return f(key)
  10. }

GO语言中的接口怎么用?

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

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

  1. type MyInterface interface {
  2. Method1() string
  3. Method2(int) int
  4. }

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

  1. type MyType struct {
  2. // type fields
  3. }
  4. func (t *MyType) Method1() string {
  5. return "Hello, world!"
  6. }
  7. func (t *MyType) Method2(n int) int {
  8. return n * n
  9. }

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

  1. func MyFunction(i MyInterface) {
  2. fmt.Println(i.Method1())
  3. fmt.Println(i.Method2(8))
  4. }

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

  1. func main() {
  2. t := &MyType{}
  3. MyFunction(t)
  4. }

调用后即可产生结果:

Hello, world!
64

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

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

  1. type MyInterface interface {
  2. Method1() string
  3. }
  4. type MyInterfaceFunc func()string
  5. func (f MyInterfaceFunc) Method1()string {
  6. return f()
  7. }

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

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

使用普通函数进行调用:

  1. func Dog()string{
  2. return "dog dog !"
  3. }
  4. func main() {
  5. MyFunction(MyInterfaceFunc(Dog))
  6. }

使用匿名函数进行调用:

  1. func main() {
  2. MyFunction(MyInterfaceFunc(func() string {
  3. return "hello!"
  4. }))
  5. }

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

dog dog !
hello!

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

GO源码例子

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

  1. type Handler interface {
  2. ServeHTTP(ResponseWriter, *Request)
  3. }
  4. type HandlerFunc func(ResponseWriter, *Request)
  5. func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  6. f(w, r)
  7. }

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

  1. func Handle(pattern string, handler Handler)

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

  1. func home(w http.ResponseWriter, r *http.Request) {
  2. w.WriteHeader(http.StatusOK)
  3. _, _ = w.Write([]byte("hello, index page"))
  4. }
  5. func main() {
  6. http.Handle("/home", http.HandlerFunc(home))
  7. _ = http.ListenAndServe("localhost:8000", nil)
  8. }

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

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

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

可以达到同样的效果。

以上就是GO语言中接口和接口型函数如何使用的详细内容,更多关于GO语言中接口和接口型函数如何使用的资料请关注九品源码其它相关文章!