From ab19d703e956e39528475171226c8d95f2ac773d Mon Sep 17 00:00:00 2001 From: destel Date: Thu, 11 Apr 2024 22:32:01 +0300 Subject: [PATCH] ForEach --- example_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..60d4441 --- /dev/null +++ b/example_test.go @@ -0,0 +1,26 @@ +package rill_test + +import ( + "fmt" + "math/rand" + "time" + + "github.com/destel/rill" +) + +func Example_forEach() { + items := rill.FromSlice([]string{"item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"}, nil) + + err := rill.ForEach(items, 3, func(item string) error { + randomSleep(1 * time.Second) // emulate long processing + fmt.Println(item) + return nil + }) + if err != nil { + fmt.Println(err) + } +} + +func randomSleep(max time.Duration) { + time.Sleep(time.Duration(rand.Intn(int(max)))) +}