This article explains how to implement and use iterators in Go. It covers the benefits of iterators over traditional slices, such as reduced memory usage and improved efficiency, by yielding one result at a time.
Link to the article
#go #article #iterators
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4🔥3
The biggest update is the introduction of iterators. The slices and maps packages have been enhanced with new methods for working with iterators. For example,
Chunk()
method from the slices package allows you to iterate over a slice in batches of a specified number of elements:people := People{Additionally, on the optimization front, PGO has been further enhanced, as we mentioned here, here and here. Thanks to more efficient stack usage, these optimizations can now provide a few extra percentage points of resource savings and performance improvements at no cost.
{"Gopher", 13},
{"Alice", 20},
{"Bob", 5},
{"Vera", 24},
{"Zac", 15},
}
// Chunk people into []Person 2 elements at a time.
for c := range slices.Chunk(people, 2) {
fmt.Println(c)
}
Please open Telegram to view this post
VIEW IN TELEGRAM
go.dev
Go 1.23 Release Notes - The Go Programming Language
❤6👍6🔥6