package main import "fmt" type IReader interface { Read(file string) []byte } type IWriter interface { Write(file string, data []byte) } // 接口组合, 这些默认就继承了Reader和Writer中的抽象方法 type IReadWriter interface { IReader IWriter } type ReadWriter struct { } func (rw *ReadWriter) Read(file string) []byte { return nil } func (rw *ReadWriter) Write(file string, data []byte) { } func main() { rw := new(ReadWriter) var iRw IReadWriter = rw // ok fmt.Println(iRw) }
有疑问加站长微信联系(非本文作者)