Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"time"
"github.com/samber/lo"
"github.com/xuender/kit/pools"
)
func main() {
pool := pools.New(10, func(value, num int) string {
time.Sleep(time.Millisecond)
return fmt.Sprintf("%d: %d*2=%d", num, value, value*2)
})
outputs := pool.Post(lo.Range(100))
fmt.Println(len(outputs))
}
Output: 100
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool[I, O any] struct { // contains filtered or unexported fields }
Pool Goroutine 池.
Example (Context) ¶
package main
import (
"context"
"fmt"
"time"
"github.com/samber/lo"
"github.com/xuender/kit/pools"
)
func main() {
pool := pools.New(10, func(input lo.Tuple2[context.Context, int], num int) int {
time.Sleep(time.Millisecond)
return input.B * input.B
})
inputs := lo.Map(lo.Range(100), func(num, _ int) lo.Tuple2[context.Context, int] {
return lo.T2(context.Background(), num)
})
outputs := pool.Post(inputs)
fmt.Println(len(outputs))
}
Output: 100
Example (Error) ¶
package main
import (
"errors"
"fmt"
"time"
"github.com/samber/lo"
"github.com/xuender/kit/pools"
)
func main() {
pool := pools.New(10, func(value, num int) lo.Tuple2[int, error] {
time.Sleep(time.Millisecond)
if value == 0 {
// nolint
return lo.T2(0, errors.New("divide by zero"))
}
return lo.T2[int, error](100/value, nil)
})
outputs := pool.Post(lo.Range(100))
fmt.Println(len(outputs))
}
Output: 100
Click to show internal directories.
Click to hide internal directories.