golang 结构体中的匿名接口
golang 结构体中的匿名接口
代码示例
golang 中,可以给结构体增加匿名field,可参考 unknwon 大神的书。
但,golang同时也可以给结构体定义一个匿名interface field,用法:
标准库 sort 中,有下面的写法:
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
type reverse struct {
Interface
}
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
func Reverse(data Interface) Interface {
return &reverse{data}
}
reverse结构体内嵌了一个Interface的interface,并且,提供了单独的Less函数定义。
却没有提供 Len, Swap 的定义。
首先,根据结构体内嵌其它匿名字段的定义,可以推知,理论上,调用reverse.Len, reverse.Swap,
肯定是会直接传递到 reverse.Interface.Len 和 reverse.Interface.Swap,
即,和直接调用Interface的同名函数没有任何区别。
但,reverse提供了单独的Less函数,它的实现是颠倒了i,j参数,仍然送入到Interface.Less中去,
那么,得出的结果与直接使用Interface排序的结果,肯定是相反的。
为什么如此设计?
Meaning of a struct with embedded anonymous interface?
摘录其中比较关键的解释如下:
- In this way
reverseimplements thesort.Interfaceand we can override a specific method without having to define all the others. - 结构体创建时,可以使用任何实现了
Interface的obj来初始化,参考:
package main
import "fmt"
// some interface
type Stringer interface {
String() string
}
// a struct that implements Stringer interface
type Struct1 struct {
field1 string
}
func (s Struct1) String() string {
return s.field1
}
// another struct that implements Stringer interface, but has a different set of fields
type Struct2 struct {
field1 []string
dummy bool
}
func (s Struct2) String() string {
return fmt.Sprintf("%v, %v", s.field1, s.dummy)
}
// container that can embedd any struct which implements Stringer interface
type StringerContainer struct {
Stringer
}
func main() {
// the following prints: This is Struct1
fmt.Println(StringerContainer{Struct1{"This is Struct1"}})
// the following prints: [This is Struct1], true
fmt.Println(StringerContainer{Struct2{[]string{"This", "is", "Struct1"}, true}})
// the following does not compile:
// cannot use "This is a type that does not implement Stringer" (type string)
// as type Stringer in field value:
// string does not implement Stringer (missing String method)
fmt.Println(StringerContainer{"This is a type that does not implement Stringer"})
}
golang 结构体中的匿名接口的更多相关文章
- golang 结构体嵌入和匿名成员
考虑一个二维的绘图程序,提供了一个各种图形的库,例如矩形.椭圆形.星形和轮形等几 何形状.这里是其中两个的定义 type Circle struct { X, Y, Radius int } type ...
- 六、golang中的结构体和方法、接口
结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...
- golang结构体、接口、反射
struct结构体 struct用来自定义复杂数据结构,可以包含多个字段属性,可以嵌套; go中的struct类型理解为类,可以定义方法,和函数定义有些许区别; struct类型是值类型. struc ...
- Golang结构体struct的使用(结构体嵌套, 匿名结构体等)
转自: https://studygolang.com/articles/11313 golang中是没有class的,但是有一个结构体struct,有点类似,他没有像java,c++中继承的概念,但 ...
- golang中结构体中的嵌套
package main import "fmt" type Base struct { name string } func (b *Base) m1() int { retur ...
- GO开发[五]:golang结构体struct
Go结构体struct Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继承在内的大量面向对象特性,只保留了组合(composition)这个最基础的特性 ...
- 【转】golang 结构体和方法
原文:https://www.jianshu.com/p/b6ae3f85c683 ---------------------------------------------------------- ...
- Golang结构体值的交换
Golang结构体值的交换 一.添加结构体,多if暴力 最先遇到这个问题是在比编写PUT方法的接口时遇到. (我公司编写http put方法,是先解析json至StudentInput结构体中,通过i ...
- sturct stat 结构体中 st_mode 的含义
工作中遇到 else if( (s_buf.st_mode&S_IFMT) == S_IFDIR) return 2; else if( !(s_buf.st_mode&S_IFREG ...
随机推荐
- compareTo的用法
在java编程中,我们会偶尔遇到字符串大小比较的问题,compareTo()方法很简单就实现这种功能.该方法用于判断一个字符串是否大于.等于还是小于另一个字符串.判断字符串大小的依据是根据它们在字典表 ...
- Smokeping配置完成之后出现Software error解决办法
下面是在浏览器中输入http://localhost/smokeping点击回车之后出现的错误: Software error: ERROR: creating /usr/local/smokepin ...
- 2019 Power BI最Top50面试题,助你面试脱颖而出系列<上>
距离4月还剩11天, 你是否还在投简历找工作而机会寥寥? 你是否还在四处奔波疲于面试而结果不意? ....... 知否知否, 天下武功唯快不破, 传说江湖有本Power BI 面试真香秘籍, 能助你快 ...
- 虚拟机VMware显示“内部错误”的解决方法
很有可能是VM服务没有启动,win+R services.msc 进入 “服务”,将VM相关的5个服务全部启动即可.
- numpy鸢尾花
import numpy from sklearn.datasets import load_iris #从sklearn包自带的数据集中读出鸢尾花数据集data iris_data = load_i ...
- CentOS 7安全加固
本次实验使用的centos 7 版本 一.查找系统中是否存在空密码账户 1.使用命令: awk -F: '($2==""){print $1}' /etc/shadow 直接查看. ...
- PTA2
一.题目:7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用 ...
- 学习笔记CB009:人工神经网络模型、手写数字识别、多层卷积网络、词向量、word2vec
人工神经网络,借鉴生物神经网络工作原理数学模型. 由n个输入特征得出与输入特征几乎相同的n个结果,训练隐藏层得到意想不到信息.信息检索领域,模型训练合理排序模型,输入特征,文档质量.文档点击历史.文档 ...
- hsdf -- 6.21 -- day6
差点忘写总结…… 这么快就到day6了啊 感觉前面写的总结都是苟屁,跟没有有什么区别…… 今天认真总结一下 最大的失误就是全程肝a题,最后还挂了…… 感觉今天方向偏了,由进行一次测试到仅仅是想要尽可能 ...
- 【项目经验】Mockito教程
一.教程 转载:https://blog.csdn.net/sdyy321/article/details/38757135/ 官网: http://mockito.org API文档:http:// ...