当前位置:网站首页>Interface embedded in golang struct
Interface embedded in golang struct
2022-07-03 03:54:00 【sandyznb】
Recently in to see context A strange usage was found in the underlying source code :struct Embedded inside interface,struct It has not been fully realized interface All interfaces of , I really didn't notice this usage before , I've only seen interface nesting interface Of , Decided to write down .
Go straight to the source :src\context\context.go
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
cancelCtx Definition and concrete implementation of structure
type cancelCtx struct {
Context
mu sync.Mutex // protects following fields
done atomic.Value // of chan struct{}, created lazily, closed by first cancel call
children map[canceler]struct{} // set to nil by the first cancel call
err error // set to non-nil by the first cancel call
}
func (c *cancelCtx) Value(key interface{}) interface{} {
if key == &cancelCtxKey {
return c
}
return c.Context.Value(key)
}
func (c *cancelCtx) Done() <-chan struct{} {
d := c.done.Load()
if d != nil {
return d.(chan struct{})
}
c.mu.Lock()
defer c.mu.Unlock()
d = c.done.Load()
if d == nil {
d = make(chan struct{})
c.done.Store(d)
}
return d.(chan struct{})
}
func (c *cancelCtx) Err() error {
c.mu.Lock()
err := c.err
c.mu.Unlock()
return err
}
timerCtx Definition and concrete implementation of structure
type timerCtx struct {
cancelCtx
timer *time.Timer // Under cancelCtx.mu.
deadline time.Time
}
func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
return c.deadline, true
}
Context Interface with 4 A way , however cancelCtx only 3 A way ,Deadline() It doesn't work , and timerCtx only Deadline(), other 3 One has not been realized .
I haven't seen this kind of writing in my cognition , To implement an interface is to implement all the methods of this interface , For example, in the source code emptyCtx
type emptyCtx int
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
return
}
func (*emptyCtx) Done() <-chan struct{} {
return nil
}
func (*emptyCtx) Err() error {
return nil
}
func (*emptyCtx) Value(key interface{}) interface{} {
return nil
}
func (e *emptyCtx) String() string {
switch e {
case background:
return "context.Background"
case todo:
return "context.TODO"
}
return "unknown empty Context"
}
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
At that time, I saw this usage , Directly confused , Or I have a weak foundation ....
The embedded interface As struct An anonymous member of , You can assume this struct This is the member interface An implementation of , Regardless of struct Has it been realized interface Defined function .
type Student interface {
Run()
Walk()
}
type Pupil struct {
age int
Student
}
func NewPupil(age int) *Pupil{
return &Pupil{age: age}
}
//func (p *Pupil) Run() {
//
//}
//
//func (p *Pupil) Walk(){
//
//}
func main(){
p := NewPupil(100)
var s Student = p
fmt.Printf("%#v\n",s)
}
struct Pupil It didn't come true interface Any function in , Just put Student As an anonymous member ,main The function can run normally without error , explain p It is indeed considered Student The concrete realization of .
Since there is no implementation function , How to call the interface ? The answer is Without implementation, you can't call , Once called crash. Only functions that have been implemented can be called . Noted above Run() and Walk() Let go of which ,s You can call which ...
边栏推荐
- 递归:深度优先搜索
- Open Visual Studio 2010 hangs when opening a SQL file sql file
- Dynamic programming: Longest palindrome substring and subsequence
- 『期末复习』16/32位微处理器(8086)基本寄存器
- node,npm以及yarn下载安装
- pytorch项目怎么跑?
- Commands related to the startup of redis under Linux server (installation and configuration)
- 【刷题篇】多数元素(超级水王问题)
- QSAR model establishment script based on pytoch and rdkit
- Mongodb master profile
猜你喜欢
递归:深度优先搜索
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
[MySQL] the difference between left join, right join and join
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
FileZilla client download and installation
递归:快速排序,归并排序和堆排序
【刷题篇】 找出第 K 小的数对距离
FileZilla Client下載安裝
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Docker install and start MySQL service
随机推荐
Use of sigaction
Null and undefined
Recursive use and multi-dimensional array object to one-dimensional array object
sigaction的使用
What is pytorch? Is pytorch a software?
Reflection and planning of a sophomore majoring in electronic information engineering
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Introduction to mongodb
Téléchargement et installation du client Filezilla
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Docker install and start MySQL service
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
pytorch项目怎么跑?
Web会话管理安全问题
shardingsphere动态数据源
Makefile demo
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
Write it down once Net travel management background CPU Explosion Analysis
Cnopendata China Customs Statistics