Skip to content

Instantly share code, notes, and snippets.

@kuwa72
Created September 26, 2024 15:12
Show Gist options
  • Save kuwa72/5bf19808c65ee5ea188e297bd9ef09e9 to your computer and use it in GitHub Desktop.
Save kuwa72/5bf19808c65ee5ea188e297bd9ef09e9 to your computer and use it in GitHub Desktop.
use sembar/lo in realworld
package main
import (
"fmt"
"github.com/samber/lo"
)
type User struct {
ID int
}
func main() {
users := []User{{1}, {2}, {3}}
fmt.Println(old(users))
fmt.Println(new(users))
}
func old(users []User) []int {
ids := make([]int, 0, len(users))
for _, v := range users {
ids = append(ids, v.ID)
}
return ids
}
func new(users []User) []int {
return lo.Map(users, func(u User, _ int) int { return u.ID })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment