Skip to content

Commit ee2d0e0

Browse files
Al2Klimovoxzi
authored andcommitted
utils.IterateOrderedMap: Use as intended, via for:=range
Require Go 1.23 Imported from Icinga/icinga-notifications@ac235b7
1 parent f918734 commit ee2d0e0

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

utils/utils.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lib/pq"
1010
"github.com/pkg/errors"
1111
"golang.org/x/exp/utf8string"
12+
"iter"
1213
"net"
1314
"os"
1415
"path/filepath"
@@ -170,11 +171,7 @@ func PrintErrorThenExit(err error, exitCode int) {
170171
//
171172
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type
172173
// is cmp.Ordered.
173-
//
174-
// Please note that currently - being at Go 1.22 - rangefuncs are still an experimental feature and cannot be directly
175-
// used unless compiled with `GOEXPERIMENT=rangefunc`. However, they can still be invoked normally.
176-
// https://go.dev/wiki/RangefuncExperiment
177-
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) func(func(K, V) bool) {
174+
func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
178175
keys := make([]K, 0, len(m))
179176
for key := range m {
180177
keys = append(keys, key)

utils/utils_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,10 @@ func TestIterateOrderedMap(t *testing.T) {
8585
for _, tt := range tests {
8686
t.Run(tt.name, func(t *testing.T) {
8787
var outKeys []int
88-
89-
// Either run with GOEXPERIMENT=rangefunc or wait for rangefuncs to land in the next Go release.
90-
// for k, _ := range IterateOrderedMap(tt.in) {
91-
// outKeys = append(outKeys, k)
92-
// }
93-
94-
// In the meantime, it can be invoked as follows.
95-
IterateOrderedMap(tt.in)(func(k int, v string) bool {
88+
for k, v := range IterateOrderedMap(tt.in) {
9689
assert.Equal(t, tt.in[k], v)
9790
outKeys = append(outKeys, k)
98-
return true
99-
})
91+
}
10092

10193
assert.Equal(t, tt.outKeys, outKeys)
10294
})

0 commit comments

Comments
 (0)