当存在双重接收者时,如何查看方法的接收者?

huangapple go评论74阅读模式
英文:

how to see method's receiver when there are double

问题

我正在尝试理解这个例子。
Ccc方法是aaa的方法还是bbb的方法,还是aaa.bbb()的方法?

当我去GitHub上点击Ccc时,我看到了一堆定义,不知道该看哪里,非常不方便。

ans := aaa.Bbb().Ccc()

真实例子
https://github.com/CyCoreSystems/ari/blob/master/_examples/play/main.go

sub := cl.Bus().Subscribe(nil, "StasisStart")

英文:

I am trying to understand this example.
Is Ccc method of aaa OR bbb or aaa.bbb().

When I go to github, and click on Ccc, I see bunch of Definitions and it's very inconvenience not knowing where to look.

ans := aaa.Bbb().Ccc()

Real example
https://github.com/CyCoreSystems/ari/blob/master/_examples/play/main.go

sub := cl.Bus().Subscribe(nil, "StasisStart")

答案1

得分: 1

Ccc()Bbb() 返回的类型的一个方法。这段代码:

ans := aaa.Bbb().Ccc()

与这段代码是等价的:

temp := aaa.Bbb()
ans := temp.Ccc()
英文:

> I am trying to understand this example. Is Ccc method of aaa OR bbb or aaa.bbb().

Ccc() is a method of whatever type Bbb() returns. This code:

ans := aaa.Bbb().Ccc()

Is the same as this code:

temp := aaa.Bbb()
ans := temp.Ccc()

答案2

得分: 1

> 真实示例 https://github.com/CyCoreSystems/ari/blob/master/_examples/play/main.go
sub := cl.Bus().Subscribe(nil, "StasisStart")

在这个示例中,

cl 是一个实现了 Client 接口的 Client 结构体,该接口具有 Bus() 方法。

Client 接口中的 Bus() 方法签名为:
Bus() Bus

因此,cl.Bus() 返回了 Bus 接口的某个实现。

Bus 接口封装了 Subscriber 接口,该接口具有以下方法:

Subscribe(key *Key, n ...string) Subscription

这就是为什么如果你手头有 Bus 接口,就可以访问 Subscribe(..) 方法。

希望我对你理解这个“真实示例”有所帮助。

英文:

> Real example https://github.com/CyCoreSystems/ari/blob/master/_examples/play/main.go
sub := cl.Bus().Subscribe(nil, "StasisStart")

In this example

cl is Client struct that implementing Client interface that has Bus() method.

Bus method signiture under Client interface is:
Bus() Bus

Therefore cl.Bus() return some implementation of Bus interface.

Bus interface is encapsulate Subscriber interface that has the following method:

Subscribe(key *Key, n ...string) Subscription

That's why you can access to Subscribe(..) method if you have Bus interface in your hands

I hope I helped to understand the "real example"

huangapple
  • 本文由 发表于 2022年1月26日 23:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70865594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定