当前位置:网站首页>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 ...
边栏推荐
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- pytorch是什么?pytorch是一个软件吗?
- What can learning pytorch do?
- Hutool动态添加定时任务
- Is pytorch difficult to learn? How to learn pytorch well?
- C语言HashTable/HashSet库汇总
- [embedded module] OLED display module
- 2022 tea master (primary) examination questions and tea master (primary) examination question bank
- 动态规划:最长公共子串和最长公共子序列
- FileZilla Client下載安裝
猜你喜欢

小程序获取用户头像和昵称

编译文件时报错:错误: 编码GBK的不可映射字符

如何迈向IPv6之IPv6过渡技术-尚文网络奎哥

2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =

第十届中国云计算大会·中国站:展望未来十年科技走向

Téléchargement et installation du client Filezilla

Recursion: quick sort, merge sort and heap sort

递归:一维链表和数组

2022 tea master (primary) examination questions and tea master (primary) examination question bank

Recursion: one dimensional linked lists and arrays
随机推荐
Introduction à mongodb
CEPH Shangwen network xUP Nange that releases the power of data
docker安装及启动mysql服务
FileZilla client download and installation
[embedded module] OLED display module
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Half of 2022 is over, so we must hurry up
For instruction, uploading pictures and display effect optimization of simple wechat applet development
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
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
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Applet (continuous update)
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
Arlo's thinking about himself
阿洛对自己的思考
毕设-基于SSM宠物领养中心
递归:深度优先搜索
node,npm以及yarn下载安装
Web session management security issues
8.8.2-PointersOnC-20220214