Home >Backend Development >Golang >What's the Most Idiomatic Way to Create Iterators in Go?

What's the Most Idiomatic Way to Create Iterators in Go?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-01 17:32:14441browse

What's the Most Idiomatic Way to Create Iterators in Go?

The Idiomatic Way to Construct Iterators in Go

Channels provide a channel-like mechanism for iteration. While they have limitations, such as not being able to break out of iteration without risking goroutine leaks, they are not the idiomatic way to create iterator patterns in Go. For this purpose, closures are more appropriate.

package main

import "fmt"

func main() {
    gen := newEven()
    fmt.Println(gen())
    fmt.Println(gen())
    fmt.Println(gen())
    gen = nil // release for garbage collection
}

func newEven() func() int {
    n := 0
    // closure captures variable n
    return func() int {
        n += 2
        return n
    }
}

Another option is to use named types with methods.

package main

import "fmt"

func main() {
    gen := even(0)
    fmt.Println(gen.next())
    fmt.Println(gen.next())
    fmt.Println(gen.next())
}

type even int

func (e *even) next() int {
    *e += 2
    return int(*e)
}

Chaining generators is straightforward due to Go's first-class functions. Here's an example:

package main

import "fmt"

func main() {
    gen := mapInt(newEven(), square)
    fmt.Println(gen())
    fmt.Println(gen())
    fmt.Println(gen())
    gen = nil // release for garbage collection
}

type intGen func() int

func newEven() intGen {
    n := 0
    return func() int {
        n += 2
        return n
    }
}

func mapInt(g intGen, f func(int) int) intGen {
    return func() int {
        return f(g())
    }
}

func square(i int) int {
    return i * i
}

In conclusion, while there are multiple techniques for creating iterators in Go, the idiomatic approach involves closures or named types with methods. The choice between these depends on specific needs and tradeoffs.

The above is the detailed content of What's the Most Idiomatic Way to Create Iterators in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn