Last active
May 10, 2023 18:33
-
-
Save rohithreddykota/66e04f9aada9f3ef43c751cb7f109a35 to your computer and use it in GitHub Desktop.
exgo with gocron example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/go-co-op/gocron" | |
"github.com/reactivex/rxgo/v2" | |
"time" | |
) | |
func main() { | |
ch := make(chan rxgo.Item) | |
s := gocron.NewScheduler(time.UTC) | |
_, _ = s.Every(10).Seconds().Do(func() { | |
data := time.Now().UnixNano() / 1000 % 100 | |
fmt.Printf("writing data to channel: %v\n", data) | |
ch <- rxgo.Of(data) | |
}) | |
s.StartAsync() | |
observable := rxgo.FromChannel(ch). | |
Map(func(ctx context.Context, i interface{}) (interface{}, error) { | |
//fmt.Println(i.(int)) | |
value := i.(int64) * 2 | |
return value, nil | |
}). | |
Filter(func(i interface{}) bool { | |
return i.(int64) < 50 | |
}) | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | |
defer cancel() | |
observable.Connect(ctx) | |
for items := range observable.Observe() { | |
fmt.Println("data received :: ", items.V) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment