golang中值类型的嵌入式字段和指针类型的嵌入式字段

总结:

  1. 值类型的嵌入式字段,该类型拥有值类型的方法集,没有值指针类型的方法集

  2. 指针类型的嵌入式字段,该类型拥有值指针类型的方法集,没有值类型的方法集,并且,该类型的指针类型也有值指针类型的方法集

有点绕,见案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
 
import "fmt"
 
type Boss struct{}
func (b *Boss) AssignWork() {
    fmt.Println("Boss assigned work")
}
 
type Manager struct{}
func (m *Manager) PreparePowerPoint() {
    fmt.Println("PowerPoint prepared")
}
 
type BossManager struct {
    Boss  // 嵌入式字段
    Manager  // 嵌入式字段
}
type BossManagerUsingPointers struct {
    *Boss  // 指针类型的嵌入式字段
    *Manager  // 指针类型的嵌入式字段
}
/*
BossManager和BossManagerUsingPointers的区别?
BossManage类型没有实现这个PromotionMaterial接口,BossManage的指针类型实现了这个接口
BossManagerUsingPointers类型实现了PromotionMaterial这个接口
BossManagerUsingPointers的指针类型也是PromotionMaterial这个接口类型
*/
 
// Define an interface that requires both methods.
type PromotionMaterial interface {
    AssignWork()
    PreparePowerPoint()
}
 
func promote(pm PromotionMaterial) {
    fmt.Println("Promoted a person with promise.")
}
 
func main() {
    bm := BossManager{}
 
    // Both methods (which use pointer receivers) have been promoted to BossManager.
    bm.AssignWork()        // "Boss assigned work"
    bm.PreparePowerPoint() // "PowerPoint prepared"
 
    // However, the method set of BossManager does not include either method because:
    // 1) {Boss, Manager} are embedded as value types, not pointer types.
    // 2) This makes it so that only the pointer type *BossManager includes both methods
    // in its method set, thus making it implement interface PromotionMaterial.
 
    // promote(bm)  // Would fail with: cannot use bm (type BossManager) as type PromotionMaterial in argument to promote:
    // BossManager does not implement PromotionMaterial (AssignWork method has pointer receiver)
    // This would work if {Boss, Manager} were embedded as pointer types.
 
    promote(&bm) // Works
 
    // Lets use the struct with the embedded pointer types:
    bm2 := BossManagerUsingPointers{}
    bm2.AssignWork()        // "Boss assigned work"
    bm2.PreparePowerPoint() // "PowerPoint prepared"
    promote(bm2)            // Works
    promote(&bm2)           // Also works, since both BossManagerUsingPointers (value type) and *BossManagerUsingPointers (pointer type)
    // method sets include both methods.
}

  

参考网址:https://unitstep.net/blog/2015/09/16/golang-promoted-methods-method-sets-and-embedded-types/

 

posted @   专职  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示