接口基础 #
一、接口概述 #
接口是一组方法签名的集合,定义了对象的行为。
1.1 接口特点 #
- 隐式实现
- 鸭子类型
- 多态支持
- 解耦依赖
二、接口定义 #
2.1 基本语法 #
go
type Reader interface {
Read(p []byte) (n int, err error)
}
2.2 多方法接口 #
go
type ReadWriter interface {
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
}
2.3 空接口 #
go
type Any interface{} // 或 interface{}
var x Any
x = 42
x = "hello"
x = []int{1, 2, 3}
三、接口实现 #
3.1 隐式实现 #
Go的接口是隐式实现的:
go
type Reader interface {
Read() string
}
type File struct {
content string
}
func (f File) Read() string {
return f.content
}
var r Reader = File{content: "data"} // File隐式实现了Reader
3.2 实现条件 #
类型必须实现接口的所有方法:
go
type Writer interface {
Write([]byte) (int, error)
}
type FileWriter struct{}
func (f FileWriter) Write(data []byte) (int, error) {
return len(data), nil
}
var w Writer = FileWriter{} // 正确
3.3 指针接收者 #
go
type Counter interface {
Increment()
}
type MyCounter struct {
value int
}
func (c *MyCounter) Increment() {
c.value++
}
var counter Counter = &MyCounter{} // 正确
// var counter Counter = MyCounter{} // 错误
四、接口变量 #
4.1 接口值 #
接口变量包含两部分:
- 类型信息
- 值信息
go
var r Reader
fmt.Printf("%T, %v\n", r, r) // <nil>, <nil>
4.2 赋值 #
go
var r Reader
r = File{content: "data"}
fmt.Printf("%T, %v\n", r, r) // main.File, {data}
4.3 nil接口 #
go
var r Reader
fmt.Println(r == nil) // true
var f *File
r = f
fmt.Println(r == nil) // false(接口值不为nil)
五、接口组合 #
5.1 接口嵌入 #
go
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type ReadWriter interface {
Reader
Writer
}
5.2 组合示例 #
go
type Closer interface {
Close() error
}
type ReadCloser interface {
Reader
Closer
}
type WriteCloser interface {
Writer
Closer
}
type ReadWriteCloser interface {
Reader
Writer
Closer
}
六、标准库接口 #
6.1 io.Reader #
go
type Reader interface {
Read(p []byte) (n int, err error)
}
6.2 io.Writer #
go
type Writer interface {
Write(p []byte) (n int, err error)
}
6.3 fmt.Stringer #
go
type Stringer interface {
String() string
}
type Person struct {
Name string
}
func (p Person) String() string {
return "Person: " + p.Name
}
fmt.Println(Person{Name: "Alice"}) // Person: Alice
6.4 error接口 #
go
type error interface {
Error() string
}
七、实际应用 #
7.1 存储接口 #
go
type Storage interface {
Save(key string, value []byte) error
Load(key string) ([]byte, error)
Delete(key string) error
}
type FileStorage struct{}
func (f FileStorage) Save(key string, value []byte) error {
return os.WriteFile(key, value, 0644)
}
func (f FileStorage) Load(key string) ([]byte, error) {
return os.ReadFile(key)
}
func (f FileStorage) Delete(key string) error {
return os.Remove(key)
}
7.2 数据库接口 #
go
type Database interface {
Connect() error
Query(sql string) ([]Row, error)
Close() error
}
7.3 日志接口 #
go
type Logger interface {
Debug(msg string)
Info(msg string)
Error(msg string)
}
八、接口设计原则 #
8.1 小接口 #
go
// 好:小接口
type Reader interface {
Read(p []byte) (n int, err error)
}
// 避免:大接口
type BigInterface interface {
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
Close() error
Flush() error
Seek(offset int64, whence int) (int64, error)
}
8.2 接口隔离 #
go
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type Closer interface {
Close() error
}
8.3 接受接口,返回具体类型 #
go
func Process(r Reader) *Result { // 接受接口
// ...
return &Result{} // 返回具体类型
}
九、总结 #
接口要点:
| 特性 | 说明 |
|---|---|
| 定义 | type Name interface |
| 实现 | 隐式实现 |
| 空接口 | interface{} |
| 组合 | 接口嵌入 |
关键点:
- 隐式实现:无需显式声明
- 鸭子类型:有方法就是实现了接口
- 小接口:接口越小越灵活
- 接口值:包含类型和值两部分
- nil接口:nil接口和值为nil的接口不同
准备好学习接口实现了吗?让我们进入下一章!
最后更新:2026-03-26