英文:
Global EventBus in GoLang
问题
我正在尝试学习GO,并尝试不同的概念。现在我正在尝试使用PubSub方法,但是在应用程序内部进行。我有一个EventBus,并且我正在尝试通过依赖注入传递实例。然而,当我运行应用程序时,什么都没有发生。
主要代码如下:
package main
import (
"github.com/asaskevich/EventBus"
modelA "interfaces/internal/modelA"
modelB "interfaces/internal/modelB"
)
func main() {
bus := EventBus.New()
a := &modelA.Bus{EventBus: bus}
a.Send()
b := &modelB.Bus{
EventBus: bus,
}
b.Receive()
}
internal/modelA代码如下:
package modelA
import (
"fmt"
"github.com/asaskevich/EventBus"
)
type Bus struct {
EventBus EventBus.Bus
}
type ModelAService interface {
Run()
Send()
}
func calculator(a int, b int) {
fmt.Printf("ModelA %d\n", a+b)
}
func (bus *Bus) Receive() {
err := bus.EventBus.Subscribe("testMessageFromB", calculator)
if err != nil {
fmt.Printf("Error Receiving message...")
}
}
func (bus *Bus) Send() {
bus.EventBus.Publish("testMessageFromA", 33, 33)
}
internal/modelB代码如下:
package modelB
import (
"fmt"
"github.com/asaskevich/EventBus"
)
type Bus struct {
EventBus EventBus.Bus
}
type ModelBService interface {
Run()
Send()
}
func calculator(a int, b int) {
fmt.Printf("ModelB %d\n", a+b)
}
func (bus *Bus) Receive() {
err := bus.EventBus.Subscribe("testMessageFromA", calculator)
if err != nil {
fmt.Printf("Error Receiving message...")
}
}
func (bus *Bus) Send() {
bus.EventBus.Publish("testMessageFromB", 33, 60)
}
英文:
I am trying to learn GO and in doing so trying different concepts. Right now I am trying a PubSub approach, but within the application. I have an EventBus and I amd trying to pass the instance via Dependency Injection. However when I run the application nothing happens.
main
package main
import (
"github.com/asaskevich/EventBus"
modelA "interfaces/internal/modelA"
modelB "interfaces/internal/modelB"
)
func main() {
bus := EventBus.New()
a := &modelA.Bus{EventBus: bus}
a.Send()
b := &modelB.Bus{
EventBus: bus,
}
b.Receive()
}
internal/modelA
package modelA
import (
"fmt"
"github.com/asaskevich/EventBus"
)
type Bus struct {
EventBus EventBus.Bus
}
type ModelAService interface {
Run()
Send()
}
func calculator(a int, b int) {
fmt.Printf("ModelA "+"%d\n", a+b)
}
func (bus *Bus) Receive() {
err := bus.EventBus.Subscribe("testMessageFromB", calculator)
if err != nil {
fmt.Printf("Error Receiving message...")
}
}
func (bus *Bus) Send() {
bus.EventBus.Publish("testMessageFromA", 33, 33)
}
internal/modelB
package modelB
import (
"fmt"
"github.com/asaskevich/EventBus"
)
type Bus struct {
EventBus EventBus.Bus
}
type ModelBService interface {
Run()
Send()
}
func calculator(a int, b int) {
fmt.Printf("ModelB "+"%d\n", a+b)
}
func (bus *Bus) Receive() {
err := bus.EventBus.Subscribe("testMessageFromA", calculator)
if err != nil {
fmt.Printf("Error Receiving message...")
}
}
func (bus *Bus) Send() {
bus.EventBus.Publish("testMessageFromB", 33, 60)
}
答案1
得分: 2
你需要首先订阅一个主题,然后发布(执行为主题定义的回调函数)。
可以尝试以下代码:
func main() {
bus := EventBus.New()
a := &modelA.Bus{EventBus: bus}
b := &modelB.Bus{EventBus: bus}
b.Receive() // 订阅
a.Send() // 发布
// 取消订阅
}
还可以参考示例:
func calculator(a int, b int) {
fmt.Printf("%d\n", a + b)
}
func main() {
bus := EventBus.New()
bus.Subscribe("main:calculator", calculator)
bus.Publish("main:calculator", 20, 40)
bus.Unsubscribe("main:calculator", calculator)
}
我的调试结构和输出如下:
注释:
为了清晰起见,你可以将b.Receive()
重命名为b.Subscribe()
,将a.Send()
重命名为a.Publish()
。
另请参阅gRPC:
英文:
You need to first Subscribe to a topic
then Publish (executes callback defined for a topic).
Try something like this:
func main() {
bus := EventBus.New()
a := &modelA.Bus{EventBus: bus}
b := &modelB.Bus{EventBus: bus}
b.Receive() // Subscribe
a.Send() // Publish
// Unsubscribe
}
Also see the example:
func calculator(a int, b int) {
fmt.Printf("%d\n", a + b)
}
func main() {
bus := EventBus.New();
bus.Subscribe("main:calculator", calculator);
bus.Publish("main:calculator", 20, 40);
bus.Unsubscribe("main:calculator", calculator);
}
My debugging structure and output:
Footnotes:
You may rename b.Receive()
to b.Subscribe()
and a.Send()
to a.Publish()
for clarity.
See also gRPC:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论