我正在尝试学习 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()
}
内部/模型A
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)
}
内部/模型B
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)
}
呼唤远方
相关分类