我正在努力学习围棋,并尝试不同的概念。现在我正在尝试一种PubSub方法,但是在应用程序中。我有一个EventBus,我试图通过依赖注入来传递实例。然而,当我运行应用程序时,什么都没有发生。
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)
}