-
方法接收者决定接口实现
type I interface{ M() } type TA struct{} func (t *TA) M() {} // 方法接收者是 *TA- *TA 实现了接口 I
- TA 的值类型 没有 实现接口 I
-
结构体嵌入的继承规则
type TB struct{ TA } // 嵌入的是 TA(值类型)- TB 的方法集包含 TA 的方法集(但 TA 本身没有 M() 方法)
- *TB 的方法集包含 *TA 的方法集(包含 M() 方法)
go文档——Struct_types,有这么一段关键信息:
Given a struct type
Sand a type nameT, promoted methods are included in the method set of the struct as follows:
- If
Scontains an embedded fieldT, the method sets ofSand*Sboth include promoted methods with receiverT. The method set of*Salso includes promoted methods with receiver*T.- If
Scontains an embedded field*T, the method sets ofSand*Sboth include promoted methods with receiverTor*T.
明确说明了当S嵌套T时,*S类型的方法集除了包含T的方法集,还包含*T的方法集。