GO语言中接口和接口型函数的具体使用
作者:雨雨不怕雨 时间:2023-06-18 09:08:40
前言
今天在编码中,看到了一个非常经典的接口用法如下,于是查阅了相关资料,发现此种写法为接口型函数,本文对此做了细致的阐述。
// 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)
}
可以达到同样的效果。
来源:https://blog.csdn.net/doreen211/article/details/129340313
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现读取文件夹按数字排序功能
关于vue的element-ui web端引入高德地图并获取经纬度
![](https://img.aspxhome.com/file/2023/6/139786_0s.png)
一个有趣的SQL命题 用一条语句切换BIT型的真假值
详解Python GUI工具取色器
![](https://img.aspxhome.com/file/2023/4/87764_0s.gif)
OpenCV学习之图像加噪与滤波的实现详解
![](https://img.aspxhome.com/file/2023/0/105040_0s.png)
Javascript Closures (1)
SQL 新增/修改 表字段列的类型等
python贪婪匹配以及多行匹配的实例讲解
json 转 mot17数据格式的实现代码 (亲测有效)
![](https://img.aspxhome.com/file/2023/8/120898_0s.png)
python爬虫神器Pyppeteer入门及使用
![](https://img.aspxhome.com/file/2023/4/77564_0s.jpg)
pycharm 设置项目的根目录教程
![](https://img.aspxhome.com/file/2023/0/90450_0s.jpg)
IE7的web标准之道 Ⅰ
![](https://img.aspxhome.com/file/UploadPic/20088/13/200881312500620s.gif)
浅谈Go语言中的结构体struct & 接口Interface & 反射
python实现读取并显示图片的两种方法
python使用新浪微博api上传图片到微博示例
详解Python命令行解析工具Argparse
Python入门篇之字符串
vue2 利用echarts 单独绘制省份的步骤
![](https://img.aspxhome.com/file/2023/3/139853_0s.png)
session 加入mysql库的方法
![](https://img.aspxhome.com/file/2023/9/118439_0s.png)