Deferred value that may be present in the future.
package main
import (
"github.com/d-exclaimation/gocurrent/future"
"github.com/d-exclaimation/gocurrent/result"
. "github.com/d-exclaimation/gocurrent/types"
"log"
"time"
)
func main() {
fut0 := future.Async(func() (Any, error) {
time.Sleep(10 * time.Second)
return 10, nil
})
fut1 := future.Map(fut0, func(any Any) Any {
return any.(int) + 2
})
future.OnComplete(fut1, result.Case{
Success: func(i Any) {
log.Println(i)
},
Failure: func(err error) {
log.Fatalln(err)
},
})
// ... wait for 10 seconds
// log: 12
}
Time based single topic data stream with a single upstream and multiple downstream (Broadcast / hot stream).
package main
import (
"github.com/d-exclaimation/gocurrent/streaming/jet"
. "github.com/d-exclaimation/gocurrent/types"
"log"
)
func main() {
jt := jet.New()
jt.On(func(i Any) {
log.Printf("[1]: %v, ", i)
})
go func() {
ch := jt.Sink()
for {
select {
case i := <-ch:
log.Printf("[2]: %v, ", i)
}
}
}()
go func() {
for jt.Next() {
log.Printf("[3]: %v\n", jt.Value())
}
}()
jt.Up(1)
// log: [1]: 1, [2]: 1, [3]: 1
jt.Up(2)
// log: [1]: 2, [2]: 2, [3]: 2
jt.Up(3)
// log: [1]: 2, [2]: 2, [3]: 2
jt.Close()
}