Skip to content

Commit

Permalink
Create main.go
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Mosciszko <[email protected]>
  • Loading branch information
Raezil authored Feb 20, 2025
1 parent ee48ad5 commit 7911dc7
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"log"

gbus "github.com/Raezil/GoEventBus"

_ "github.com/lib/pq"

Check failure on line 9 in examples/1/main.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/lib/pq; to add it:
)

// HouseWasSold represents an event for when a house has been sold
type HouseWasSold struct{}

// NewDispatcher initializes the dispatcher with event handlers
func NewDispatcher() *gbus.Dispatcher {
return &gbus.Dispatcher{
HouseWasSold{}: func(m map[string]interface{}) (gbus.Result, error) {
price, ok := m["price"].(int)
if !ok {
return gbus.Result{}, fmt.Errorf("price not provided or invalid")
}
result := fmt.Sprintf("House was sold for %d", price)
log.Println(result)
return gbus.Result{
Message: result,
}, nil
},
}
}

func main() {
// Initialize dispatcher and event store
dispatcher := NewDispatcher()
eventstore := gbus.NewEventStore(dispatcher)
eventstore.Publish(gbus.NewEvent(
HouseWasSold{},
map[string]interface{}{
"price": 100,
},
))

eventstore.Publish(gbus.NewEvent(
HouseWasSold{},
map[string]interface{}{
"price": 100,
},
))
// Broadcast the event
eventstore.Broadcast()

}

0 comments on commit 7911dc7

Please sign in to comment.