Created
September 26, 2024 15:12
-
-
Save kuwa72/5bf19808c65ee5ea188e297bd9ef09e9 to your computer and use it in GitHub Desktop.
use sembar/lo in realworld
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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