GoReactive is a Go library for Function Reactive Programming, a library for representing and consuming asyncronous data streams with Observables.
At it's simplest form, an Observable
is an interface which allows you to subscribe to events, the next value, the completion or the failure.
observable.Subscribe(
func(value interface{}) { /* a new value has been sent */ },
func() { /* the stream has completed */ },
func(err error) { /* the stream has errored */ }
)
Using Map
, you can transform an Observables next values. For example, we can use the following to create a new Observable
from an Observable
of integers by multiplying their value.
transformedObservable := Map(observable, func(value interface{}) interface{} {
return value.(int) * 2
})
Using Filter
and Exclude
you can filter or exclude next values from an Observable
. We can use the following to create a new Observable
from an Observable
of integers, filtering for all even numbers.
filteredObservable := Filter(observable, func(value interface{}) bool {
return (value.(int) % 2) == 0
})
A Subject
is an interface for creating Observables, a Subject
contains functions to emit new values into your Observable.
subject.SendNext("Kyle")
subject.SendNext("Katie")
subject.SendCompletion()
subject.SendError(err)
NewObservable
is a function for creating an Observable, which when subscribed, it will call a callback allowing you to send events using a Subject
.
observable := NewObservable(func(subject *Subject) Disposable {
subject.SendNext("Hello, my subscriber.")
return nil
})
GoReactive is licensed under the BSD license. See LICENSE for more info.