How about add a Join function?
Author: donnolCreated Jan 6, 2023Updated Jun 21, 2026
Something like:
package lo
func Join[J, K, R any](
left []J,
right []K,
match func(J, K) bool,
mapper func(J, K) R,
) []R {
var r []R
for _, j := range left {
for _, k := range right {
if !match(j, k) {
continue
}
r = append(r, mapper(j, k))
}
}
return r
}Usage:
type User struct {
Id uint64
Name string
}
type Book struct {
Id uint64
Title string
Author uint64 // User.Id
}
type BookWithUser struct {
Book
UserName string
}
func UserBookMatcher(j User, k Book) bool {
return j.Id == k.Author
}
func IntMatcher(j, k int) bool {
return j == k
}
func main() {
r := Join([]User{}, []Book{}, UserBookMatcher, func(j User, k Book) BookWithUser {
return BookWithUser {
Book: k,
UserName: j.Name,
}
})
fmt.Println(r)
}Source: samber/lo