github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/loopexamples/loop_ToMapResolv_test.go (about) 1 package loopexamples 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/m4gshm/gollections/loop" 9 "github.com/m4gshm/gollections/map_/resolv" 10 "github.com/m4gshm/gollections/op" 11 ) 12 13 func Test_ToMapResolv(t *testing.T) { 14 15 var ageGroupedSortedNames map[string][]string 16 17 ageGroupedSortedNames = loop.ToMapResolv(loop.Of(users...), func(u User) string { 18 return op.IfElse(u.age <= 30, "<=30", ">30") 19 }, User.Name, resolv.SortedSlice) 20 21 //map[<=30:[Bob Tom] >30:[Alice Chris]] 22 23 assert.Equal(t, []string{"Bob", "Tom"}, ageGroupedSortedNames["<=30"]) 24 assert.Equal(t, []string{"Alice", "Chris"}, ageGroupedSortedNames[">30"]) 25 }