### Benchmark Results Comparison in Go Source: https://lo.samber.dev/llms.txt Presents example benchmark results comparing the `lo.Uniq` function with a manual implementation. This highlights performance differences in terms of operations per second, memory allocation, and operations per allocation, guiding optimization choices. ```go // Example benchmark results for lo.Uniq vs manual implementation // BenchmarkLoUniq-8 1000000 1200 ns/op 800 B/op 4 allocs/op // BenchmarkManualUniq-8 5000000 240 ns/op 0 B/op 0 allocs/op (when in-place) ``` -------------------------------- ### Basic Collection Operations in Go Source: https://lo.samber.dev/llms.txt Demonstrates fundamental collection manipulation using the `lo` library, including removing duplicates from slices and performing combined filtering and transformation operations. These functions are useful for common data cleaning and preparation tasks. ```go import "github.com/samber/lo" import "fmt" // Remove duplicates from a slice names := lo.Uniq([]string{"Samuel", "John", "Samuel"}) // []string{"Samuel", "John"} // Filter and transform in one operation numbers := lo.FilterMap([]int{1, 2, 3, 4, 5}, func(item int, index int) (string, bool) { if item%2 == 0 { return fmt.Sprintf("even-%d", item), true } return "", false }) // []string{"even-2", "even-4"} ``` -------------------------------- ### Concurrency and Async Operations in Go with Lo Source: https://lo.samber.dev/llms.txt Showcases how to leverage the `lo` library for parallel processing, debouncing operations, and safe error handling. These patterns are crucial for building responsive and resilient applications. ```go import ( "github.com/samber/lo" "time" ) // Placeholder types and functions for demonstration var items []Item var query string type Item struct {} type Result struct {} func processItem(item Item) Result { return Result{} } func searchAPI(q string) []Result { return nil } func updateUI(results []Result) {} func dangerousOperation() (int, error) { return 0, nil } // Parallel processing of large datasets results := lo.Map(items, func(item Item) Result { return processItem(item) // Executed in parallel }) // Debounce API calls to avoid rate limiting debouncedSearch := lo.NewDebounce(300*time.Millisecond, func() { results := searchAPI(query) updateUI(results) }) // Safe error handling with fallbacks result, ok := lo.TryOr(func() (int, error) { return dangerousOperation() }, 0) ``` -------------------------------- ### Functional Control Flow in Go using Lo Source: https://lo.samber.dev/llms.txt Demonstrates elegant conditional logic and robust retry mechanisms provided by the `lo` library. This includes using `lo.If`, `ElseIfF`, `Else` for conditional expressions and `lo.AttemptWithDelay` for retrying operations with backoff. ```go import ( "context" "errors" "time" "github.com/samber/lo" ) // Placeholder types and functions for demonstration var user any var isGuest bool var items []string type Order struct {} func checkInventory(items []string) struct{ Available bool } { return struct{ Available bool }{true} } func createOrder(user any, items []string) (Order, error) { return Order{}, nil } func callExternalAPI(ctx context.Context) error { return nil } // Elegant conditional logic message := lo.If(user != nil, "Welcome back!"). ElseIfF(isGuest, func() string { return "Welcome guest!" }). Else("Please login") // Retry logic with exponential backoff result, err := lo.AttemptWithDelay(3, time.Second, func(i int, d time.Duration) error { return callExternalAPI(context.Background()) }) // Transaction management tx := lo.NewTransaction[Order]() def tx.Rollback() order, err := tx.Run(func() (Order, error) { inventory := checkInventory(items) if !inventory.Available { return Order{}, errors.New("insufficient inventory") } return createOrder(user, items) }) ``` -------------------------------- ### Advanced Data Processing with Lo in Go Source: https://lo.samber.dev/llms.txt Illustrates complex data transformations and safe data access patterns using the `lo` library. This includes creating data processing pipelines with `lo.Pipe` and safely accessing nested map values with `lo.ValueOr` and `lo.CoalesceMap`. ```go import "github.com/samber/lo" type User struct { ID int; Name string; Active bool } // Complex data transformation pipeline users := []User{{ID: 1, Name: "Alice", Active: true}, {ID: 2, Name: "Bob", Active: false}} // Group active users by name length, then extract IDs activeUserIDs := lo.Pipe( users, lo.Filter(func(u User) bool { return u.Active }), lo.GroupBy(func(u User) int { return len(u.Name) }), lo.MapValues(func(users []User) []int { return lo.Map(users, func(u User) int { return u.ID }) }), ) // map[int][]int{5: []int{1}} // Safe nested data access config := map[string]any{ "database": map[string]any{ "host": "localhost", "port": 5432, }, } host := lo.ValueOr(lo.CoalesceMap( lo.MapValues(config, func(v any) map[string]any { if m, ok := v.(map[string]any); ok { return m } return nil }), ), "localhost") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.