go中interface定义
Go 语言中的接口是一组方法的组合,它是 Go 语言的重要组成部分。简单的说,interface是一组method签名的组合,我们通过interface来定义对象的一组行为。interface 是一种类型,定义如下:
type Person interface {
Eat(food string)
}它的定义可以看出来用了 type 关键字,更准确的说 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为。
golangempty interfaceinterfaceinterface。empty interfacegojavainterfaceinterface实现接口
Gotype error interface {
Error() string
}
type RPCError struct {
Code int64
Message string
}
func (e *RPCError) Error() string {
return fmt.Sprintf("%s, code=%d", e.Message, e.Code)
}errorError() stringerrorGoRPCErrorGointerface- 性能下降。使用interface作为函数参数,runtime 的时候会动态的确定行为。使用具体类型则会在编译期就确定类型。
- 不能清楚的看出struct实现了哪些接口,需要借助ide或其它工具。
两种接口
空interface
非空interface
范例
函数的传入参数实现接口
package main
import "fmt"
type Reader interface {
Read() int
}
type MyStruct struct {
X, Y int
}
func (m *MyStruct) Read() int {
return m.X + m.Y
}
func run(r Reader) {
fmt.Println(r.Read())
}
func main() {
s := &MyStruct{3, 4}
run(s)
}
输出:
7
分析:
因为*MyStruct实现了Read方法,因此&MyStruct实现了Reader接口,所以把*MyStruct作为参数调用run时候,&MyStruct{3, 4}就自动实现了Reader接口,因此run里可以用r.Read()来调用接口的方法.
函数的穿出参数实现接口
package main
import "fmt"
type Sumer interface {
Sum() int
}
type MyStruct struct {
X, Y int
}
func (this *MyStruct) Sum() int {
return this.X + this.Y
}
func New(a, b int) Sumer {
return &MyStruct{a, b}
}
func main() {
m := New(3, 4)
s := m.Sum()
fmt.Println(s)
}
输出
7
分析:
可以看出,因为*MyStruct实现了Sum方法,因此&MyStruct实现了Sumer接口;
空接口用于传入、传出参数
import "fmt"
type Reader interface {
Read() int
}
type MyStruct struct {
X, Y int
}
func (m *MyStruct) Read() int {
return m.X + m.Y
}
func run(r interface{}) {
// 会报错
//fmt.Println(r.Read())
fmt.Println(r)
fmt.Printf("%T\n", r)
}
func main() {
s := &MyStruct{3, 4}
run(s)
}
输出
&{3 4}
*main.MyStruct
分析:
如果把上面例子中注释去掉,则会报错;
./example.go:19: r.Read undefined (type interface {} is interface with no methods)