```
type People struct{
Name string
}
type Student struct{
People
School string
}
type Teacher struct{
People
Department string
}
func (p People) SayHi(){}
func (s Student) SayHi(){}
func (t Teacher) SayHi(){}
func (s Student) Study(){}
//根据struct的方法提取接口 从而使struct自动实现了该接口
type Speaker interface{
SayHi()
}
type Learner interface{
SayHi()
Study()
}
```
```
//ChunkFooter 块Footer
type ChunkFooter struct {
ChunkDataTotalSize int
}
//NewChunkFooter 创建一个ChunkFooter
func NewChunkFooter(chunkDataTotalSize int) *ChunkFooter {
var result = new(ChunkFooter)
result.ChunkDataTotalSize = chunkDataTotalSize
return result
}
```
这两段代码我是复制别人博客里面的。 首先我想理解的是,这里对于 struct 的函数扩展,一般都是会使用指针的,比如 beego 的 router:
```
type DeviceController struct {
beego.Controller
}
// URLMapping ...
func (c *DeviceController) URLMapping() {
c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
}
```
但是看到了也有直接使用类型的。 我不是很明白这样两种有什么区别? 一个是扩展到类型上? 一个是扩展到实例上?
有疑问加站长微信联系(非本文作者)