18

This problem is pretty common: an object should notify all its subscribers when some event occurs. In C++ we may use boost::signals or something else. But how to do this in Go language? It would be nice to see some working code example where a couple of objects are subscribed to a publisher and process notifications.

Thanks

Fedir RYKHTIK
  • 9,844
  • 6
  • 58
  • 68
Stas
  • 11,571
  • 9
  • 40
  • 58

2 Answers2

22

This is actually pretty simple in Go. Use channels. This is the kind of thing they're made for.

type Publish struct {
    listeners []chan *Msg
}

type Subscriber struct {
    Channel chan *Msg
}

func (p *Publisher) Sub(c chan *Msg) {
    p.appendListener(c)
}

func (p *Publisher) Pub(m *Msg) {
    for _, c := range p.listeners {
        c <- Msg
    }
}

func (s *Subscriber) ListenOnChannel() {
    for {
        data := <-s.Channel
        //Process data
    }
}

func main() {
    for _, v := range subscribers {
        p.Sub(v.Channel)
        go v.ListenOnChannel()
    }
    //Some kind of wait here
}

Obviously this isn't exactly a working code sample. But it's close.

cthom06
  • 9,389
  • 4
  • 36
  • 28
  • 3
    Be careful with blocking channel operations. – Markus Jarderot Sep 17 '10 at 12:23
  • @MizardX of course, normally I'd have control chan's as well, like chan bool and use select {}, and quit on a recv from the control channel. But that's all fairly trivial and a bit excessive for a basic example. – cthom06 Sep 17 '10 at 12:32
  • 1
    @MizardX: isn't that a bit like saying "be careful with pointer arithmetic" in C? Being careful with channels is 90% of Go programming ;-) – Steve Jessop Sep 17 '10 at 12:37
  • 8
    This is technically sending a pointer to the same message to all subscribers, which is easier on memory but potentially dangerous (mutable event). If the channel was not of type *Msg, but "chan Msg", would I then be sending copies of the same message all around? Asuming small messages, that would seem like a safer solution. – galaktor Sep 04 '12 at 07:19
  • 1
    What is `Publisher` and how is access to it synchronized? It's not trivial to implement and I'm sure that is the *real* question here. – AndreKR Feb 21 '19 at 22:46
2

Here I give a classific implementation without channels, be free to refer this post enter image description here

Assumed Example: Suppose you are interested in the stock market. You have the following needs: You want to keep track of the stock prices of a particular company (e.g. Apple Inc). You would not like to miss any stock price update especially if the price is dropping to a certain point. You would like to be notified of all the stock price updates.

interfaces:

// Subject interface
type Subject interface {
    Attach(o Observer) (bool, error)
    Detach(o Observer) (bool, error)
    Notify() (bool, error)
}

// Observer Interface
type Observer interface {
    Update(string)
}

Concrete Observer object

// Concrete Observer: StockObserver
type StockObserver struct {
    name string
}

func (s *StockObserver) Update(t string) {
    // do something
    println("StockObserver:", s.name, "has been updated,", "received subject string:", t)
}

Concrete Subject object

// Concrete Subject: stockMonitor
type StockMonitor struct {
    // internal state
    ticker string
    price  float64

    observers []Observer
}

func (s *StockMonitor) Attach(o Observer) (bool, error) {

    for _, observer := range s.observers {
        if observer == o {
            return false, errors.New("Observer already exists")
        }
    }
    s.observers = append(s.observers, o)
    return true, nil
}

func (s *StockMonitor) Detach(o Observer) (bool, error) {

    for i, observer := range s.observers {
        if observer == o {
            s.observers = append(s.observers[:i], s.observers[i+1:]...)
            return true, nil
        }
    }
    return false, errors.New("Observer not found")
}

func (s *StockMonitor) Notify() (bool, error) {
    for _, observer := range s.observers {
        observer.Update(s.String())
    }
    return true, nil
}

func (s *StockMonitor) SetPrice(price float64) {
    s.price = price
    s.Notify()
}

func (s *StockMonitor) String() string {
    convertFloatToString := strconv.FormatFloat(s.price, 'f', 2, 64)
    return "StockMonitor: " + s.ticker + " $" + convertFloatToString
}

main.go


func main() {

    // Create a new stockMonitor object
    stockMonitor := &StockMonitor{
        ticker: "AAPL",
        price:  0.0,
    }

    observerA := &StockObserver{
        name: "Observer A",
    }
    observerB := &StockObserver{
        name: "Observer B",
    }

    // Attach our Observers to the stockMonitor
    stockMonitor.Attach(observerA)
    stockMonitor.Attach(observerB)

    // Start the stockMonitor
    stockMonitor.Notify()

    // Change the price of the stockMonitor
    stockMonitor.SetPrice(500)

    // Detach an Observer from the stockMonitor
    stockMonitor.Detach(observerA)

    // Change the price of the stockMonitor
    stockMonitor.SetPrice(528)
}

In this part

  • We create two observers, observerA and observerB. Attach them to the stockMonitor.
  • Change the price of the stockMonitor.
  • We see that observerA and obsererB are both notified.
  • Detach observerA from the stockMonitor and change the stock price. We can see that only observerB is notified.
Jerry An
  • 1,077
  • 10
  • 18
  • Where is the implementation of the Update function on the StockObserver type? Currently no actual Update() function is called on the observer instances. – Sam Feb 03 '22 at 11:55