You can use a closure as a generator:
package main
import "fmt"
func getPositiveOdds(
numbers []int,
) (
iter func() (int, bool),
) {
iter = func() (ret int, ok bool) {
for len(numbers) > 0 {
if numbers[0] > 0 && numbers[0]&1 == 1 {
ret = numbers[
On Mon, Oct 26, 2020, 19:05 Oliver Smith
wrote:
>
> Hi Axel, thanks for replying;
>
> It isn't a pattern I teach anyone, rather it's a pattern which people I'm
> encouraging to learn golang ask me about. Frequently. I was also under the
> impression that generally passing a send-only channel to a
Hi Axel, thanks for replying;
It isn't a pattern I teach anyone, rather it's a pattern which people I'm
encouraging to learn golang ask me about. Frequently. I was also under the
impression that generally passing a send-only channel to a function could
typically be considered an indicator the
On Mon, Oct 26, 2020 at 12:29 AM Oliver Smith <
oliver.sm...@superevilmegacorp.com> wrote:
> This pattern/idiom is very un-golike in that it's an eyesore, and one of
> the hardest things I've found to teach people new to trying to read go code.
>
FWIW, I definitely disagree that this is somehow "
Looking at how C++ has chosen to provide it's users with generators served
as a bit of a wake-up call for me on how we implement them in Go. We write
them backwards:
```go
func getPositiveOdds(numbers []int) <-chan int {
channel := make(chan int)
go func () {
defer close(channel)