总结:

  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. SpringBoot 设置服务一启动就执行、初始化数据

    定义一个类实现ApplicationRunner接口,然后Override这个ApplicationRunner接口的run方法 @Component public class TaskRunner ...

  2. cmake引入第三方库的debug和release版本之Windows版本

    概述 本文将介绍cmak引入第三方库debug和release不同配置. Windows上,习惯将debug模式下生成的动态库名后缀添加D 以作和release区分.cmake创建一个项目A,A引入动 ...

  3. C语言获取文件大小(字节)

    代码 核心代码 FILE *pfile = nullptr; int ret = fopen_s(&pfile, str.c_str(), "rb"); /// 0 = 打 ...

  4. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  5. Centos/Docker/Nginx/Node/Jenkins 操作

    Centos Centos 是一个基于 Linux 的开源免费操作系统 # 本地拷贝文件到远程服务器scp output.txt root@47.93.242.155:/data/ output.tx ...

  6. Nginx 简单配置反向代理

    nginx 配置反向代理,转发请求到另一个域名 在server中加入 location /${alias} { proxy_buffer_size 128k; proxy_buffers 32 32k ...

  7. SpringCloud创建项目父工程

    1.说明 本文详解介绍Spring Cloud项目的父工程创建, 由于Spring Cloud项目下有很多模块组件, 需要先创建一个大的父工程项目, 然后在下面创建各个子工程模块. 2.创建父工程 这 ...

  8. windows环境下node安装教程(超详细)

    安装node.js 1.下载node: 下载地址:http://nodejs.cn/download/ node.js的zip包安装时是直接解压缩后就可以了, node.js的msi包是傻瓜式一路ne ...

  9. linux 设置开机自动启动应用

    作为一个开发,项目现在一般都是部署在虚拟机上的linux,数据库也是按照在l虚拟机上的linux,一旦关机了,在开机程序都没打开,又要一个个去开,很麻烦,所以现在我现在使用supervisor去做一个 ...

  10. lscpu

    [root@kvm02 ~]# lscpu Architecture: x86_64     #cpu架构CPU op-mode(s): 32-bit, 64-bitByte Order: Littl ...