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.Lenreverse.Interface.Swap

即,和直接调用Interface的同名函数没有任何区别。

但,reverse提供了单独的Less函数,它的实现是颠倒了i,j参数,仍然送入到Interface.Less中去,

那么,得出的结果与直接使用Interface排序的结果,肯定是相反的。

为什么如此设计?

Meaning of a struct with embedded anonymous interface?

摘录其中比较关键的解释如下:

  1. In this way reverse implements the sort.Interface and we can override a specific method without having to define all the others.
  2. 结构体创建时,可以使用任何实现了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 结构体中的匿名接口的更多相关文章

  1. golang 结构体嵌入和匿名成员

    考虑一个二维的绘图程序,提供了一个各种图形的库,例如矩形.椭圆形.星形和轮形等几 何形状.这里是其中两个的定义 type Circle struct { X, Y, Radius int } type ...

  2. 六、golang中的结构体和方法、接口

    结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...

  3. golang结构体、接口、反射

    struct结构体 struct用来自定义复杂数据结构,可以包含多个字段属性,可以嵌套; go中的struct类型理解为类,可以定义方法,和函数定义有些许区别; struct类型是值类型. struc ...

  4. Golang结构体struct的使用(结构体嵌套, 匿名结构体等)

    转自: https://studygolang.com/articles/11313 golang中是没有class的,但是有一个结构体struct,有点类似,他没有像java,c++中继承的概念,但 ...

  5. golang中结构体中的嵌套

    package main import "fmt" type Base struct { name string } func (b *Base) m1() int { retur ...

  6. GO开发[五]:golang结构体struct

    Go结构体struct Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继承在内的大量面向对象特性,只保留了组合(composition)这个最基础的特性 ...

  7. 【转】golang 结构体和方法

    原文:https://www.jianshu.com/p/b6ae3f85c683 ---------------------------------------------------------- ...

  8. Golang结构体值的交换

    Golang结构体值的交换 一.添加结构体,多if暴力 最先遇到这个问题是在比编写PUT方法的接口时遇到. (我公司编写http put方法,是先解析json至StudentInput结构体中,通过i ...

  9. sturct stat 结构体中 st_mode 的含义

    工作中遇到 else if( (s_buf.st_mode&S_IFMT) == S_IFDIR) return 2; else if( !(s_buf.st_mode&S_IFREG ...

随机推荐

  1. Integer与String的知试点(java中常见面试题)

    1.==符号使用 Integer a = 1000,b=1000; Integer c = 100,d=100; System.out.println(a==b); System.out.printl ...

  2. 命令行执行python模块时提示ImportError: No module named xxx

    在pycharm中运行python文件没有问题,切换到cmd中是提示:ImportError: No module named xxx 原因: pycharm在运行时会把当前工程的所有文件夹路径都作为 ...

  3. 关于html2canvas跨域设置

    最近在做js生成图片的功能,因为有的商户logo图片在阿里云oss存储的,所以遇到了跨域的问题.跨域的话可以在服务器端设置header, 跨域的话目前的解决方案是服务端设置,Access-Contro ...

  4. Sublime Text 3 package control 解决 "There are no packages available for installation" 错误

    造成这个错误的原因估计是因为由于packagecontrol.io无法访问,所以导致无法读取“https://packagecontrol.io/channel_v3.json”文件的原因. 解决方法 ...

  5. js: 文件(excel)下载处理

    以前很少接触文件下载的功能,昨天和后台开发人员联调下载功能出现了问题,一开始我请求接口,返回二进制文件流乱码,在网上找了方法,可以解决.后面后台开发人员改了一下,返回文件地址,然后就解决了.所以我了解 ...

  6. vs code 操作Git

    首次从Git拉取项目:Ctrl+Shift+p 选择Git 克隆 拉取成功后 Ctrl+波浪号进入控制台选择终端 使用npm install下载依赖 到此就从Git拉取成功了: 如果提示npm错误,有 ...

  7. 总结描述用户和组管理类命令的使用方法,系统用户相关信息,取出主机IP地址

    1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可. [root@db146 ~]# who|cut -f1 -d' ' |sort -u root 2.取出最后 ...

  8. mininet的学习之三----------mininet中流表应用实战

    我们看的同一个视频,工整的整理见以下网址 https://blog.csdn.net/weixin_36372879/article/details/86371583#commentBox

  9. Windows环境下C++中关于文件结束符的问题

    参考资料:http://www.cnblogs.com/day-dayup/p/3572374.html 一.前言 在不同的OS环境下,程序中对应的文件结束符有所不一样,根据<C++ Prime ...

  10. Dubbo的三种连接方式

    1.采用zookeeper作为注册中心 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...