总结:

  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. MyBatis学习(二)MyBatis-Statement方式的增删改查

    1.前期准备 项目骨架图如下所示 1.配置conf.xml <?xml version="1.0" encoding="UTF-8" ?> < ...

  2. JAVA实现map集合转Xml格式

    import java.util.Iterator; import java.util.SortedMap; import java.util.TreeMap; public class MainTe ...

  3. cmake之引入外部项目(引用其他项目)、FetchContent管理子模块(fetchcontent用法)

    本文CMAKE版本为3.18 演示环境: Windows+CMake+VS2017 源码下载说明 演示代码是后来传上去的,而且做了些修改,将spdlog_demo由exe改为了lib,但是,spdlo ...

  4. 【九度OJ】题目1126:打印极值点下标 解题报告

    [九度OJ]题目1126:打印极值点下标 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1126 题目描述: 在 ...

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

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

  6. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  7. 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)

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

  8. 最大流问题的Ford-Fulkerson模板

    详细讲解:http://blog.csdn.net/smartxxyx/article/details/9293665 下面贴上我的第一道最大流的题: hdu3549 1 #include<st ...

  9. 图的存储:邻接矩阵(C++)

    1. 演示 无向图: 有向网: 2. 代码 1 #include <iostream> 2 #include <unordered_map> 3 #include <ve ...

  10. github项目托管方式(看项目自身是否自带有 .git)

    一.本地仓库和远程仓库建立联系 方式一:项目自身带有 .git文件的[自身就是一个本地仓库的](这里咱以vue-cli3项目为例) 1.创建自带.git本地仓库:创建一个叫 my-vue 的项目 2. ...