总结:

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

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

有点绕,见案例:

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/

golang中值类型的嵌入式字段和指针类型的嵌入式字段的更多相关文章

  1. golang中值类型/指针类型的变量区别总结

    转自:https://segmentfault.com/a/1190000012329213 值类型的变量和指针类型的变量 先声明一个结构体: type T struct { Name string ...

  2. go中值传递、引用传递、指针传递的区别

    go语言中的值类型: int.float.bool.array.sturct等 值传递是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数 声明一个值类 ...

  3. Windows 编程中恼人的各种字符以及字符指针类型

    在Windows编程中,很容易见到这些数据类型:LPSTR,LPTSTR,LPCTSTR... 像很多童鞋一样,当初在学Windows编程的时候,对着些数据类型真的是丈二和尚,摸不着头脑,长时间不用就 ...

  4. Golang中使用lua进行扩展

    前言 最近在项目中需要使用lua进行扩展,发现github上有一个用golang编写的lua虚拟机,名字叫做gopher-lua.使用后发现还不错,借此分享给大家. 数据类型 lua中的数据类型与go ...

  5. golang中,new和make的区别

    在golang中,make和new都是分配内存的,但是它们之间还是有些区别的,只有理解了它们之间的不同,才能在合适的场合使用. 简单来说,new只是分配内存,不初始化内存: 而make即分配又初始化内 ...

  6. C语言 数组类型与数组指针类型

    //数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...

  7. Q_DECLARE_METATYPE(继承QObject的类都已经自动注册),注册后的类型可以作为QVariant的自定义类型

    简介 这个宏用来注册一个类(含默认构造.默认析构.拷贝构造函数)为QMetaType类型 ,注册后的类型可以作为QVariant的自定义类型. 这个宏应该放在类或者结构体外面的下面,也可以放在一个非公 ...

  8. [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序

    如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc

  9. Golang 中哪些值是不可以寻址的

    不可以寻址, 指的是不能通过&获得其地址. golang中不能寻址的可以总结为:不可变的,临时结果和不安全的.只要符合其中任何一个条件,它就是不可以寻址的. 具体为: 常量的值. 基本类型值的 ...

随机推荐

  1. 微信支付——沙箱调试环境getsignkey方法秘钥获取及常见问题说明

    官方文档 :https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=23_1 微信支付PC二维码支付:https://www.cnblogs ...

  2. centos使用docker 安装 rabbitMq 消息队列

    1.拉取镜像 docker pull rabbitmq:3-management 如果出现报错: Get https://registry-1.docker.io/v2/: net/http: req ...

  3. JAVA整合Redis使用redisTemplate清除库中的所有键值对数据

    JAVA整合Redis使用redisTemplate清除库中的所有键值对数据,清除所有缓存数据 Set<String> keys = redisTemplate.keys("*& ...

  4. 【LeetCode】293. Flip Game 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  5. 【LeetCode】832. Flipping an Image 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...

  6. Jmeter环境变量配置你不得不知道的事情

    在安装Jmeter的过程中大家肯定需要配置环境,但是为什么要配置JDK的环境变量呢?大家有没有好奇过,有没有仔细去像一下呢,其实在安装Jmeter前,大家应该都知道Jmeter是我们JAVA开发的,J ...

  7. C++模拟python风格的print函数--打印vector,map,list等结构

    // 最基本实现 template<typename T> static void print(T t) { std::cout << t; } // 处理 std::pair ...

  8. PL2586旺玖|USB 2.0HUB 工业级芯片|PROLIFIC PL2586

    工业级  USB 2.0 HUB 高速4端口集线器控制器 PL2586 1.PL2586说明   PL2586是USB 2.0高速4端口集线器控制器的高性能解决方案,完全符合通用串行总线规范2.0.控 ...

  9. Flutter 让你的Dialog脱胎换骨吧!(Attach,Dialog,Loading,Toast)

    前言 Q:你一生中闻过最臭的东西,是什么? A:我那早已腐烂的梦. 兄弟萌!!!我又来了! 这次,我能自信的对大家说:我终于给大家带了一个,能真正帮助大家解决诸多坑比场景的pub包! 将之前的flut ...

  10. centos8 yum安装nginx后启动不了nginx

    起动报下列错误 移动到安装目录下起动报下列错误,说是端口被占用 输入journalctl -xe命令查看,发现如下: 这个是一个什么错误,度娘一下SElinux 输入sestatus查看下SElinu ...