home / skills / dcjanus / prompts / golang-lo

golang-lo skill

/skills/golang-lo

This skill helps you use the samber/lo Go library more efficiently across collections, errors, and concurrency to boost productivity.

npx playbooks add skill dcjanus/prompts --skill golang-lo

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.3 KB
---
name: golang-lo
description: Go >= 1.18 项目中希望用 samber/lo(Lodash 风格泛型库)简化集合/映射/字符串、错误处理、重试/节流/防抖、通道并发或指针空值场景时使用。
---

# lo Go 工具库速用指南

## 快速上手
- 安装:`go get github.com/samber/lo@v1`。
- 常用导入:
```go
import (
    "github.com/samber/lo"
    lop "github.com/samber/lo/parallel"
    lom "github.com/samber/lo/mutable"
    loi "github.com/samber/lo/it"
)
```
- 常用函数速览:
```go
// Filter: 按条件保留
lo.Filter(nums, func(x int, _ int) bool { return x%2==0 })
// Map: 映射生成新切片
lo.Map(nums, func(x int, _ int) int { return x*x })
// Find: 找到首个满足条件的元素
v, ok := lo.Find(nums, func(x int) bool { return x > 10 })
// Uniq: 去重并保持顺序
uniq := lo.Uniq([]string{"a","a","b"})
// GroupBy: 按键分组
groups := lo.GroupBy(users, func(u User) int { return u.Age })
// Must: 遇 err/false panic,常用于初始化
t := lo.Must(time.Parse(time.RFC3339, ts))
```

## 官方清单获取
使用 curl 直接读取最新函数列表:

```bash
curl -sSL https://lo.samber.dev/llms.txt
```

该清单随 Git 仓库最新提交更新,可能包含尚未发布的变更;使用前请核对本地依赖版本。

Overview

This skill explains how to use the samber/lo Go utility library (v1+) to simplify common collection, string, error, concurrency, retry/throttle/debounce and pointer-null scenarios in Go projects targeting 1.18+. It focuses on practical functions and quick examples to help you replace verbose loops and nil checks with concise, type-safe helpers. It also points to the official function list source for the latest API surface.

How this skill works

The skill describes core lo functions (Map, Filter, Find, Uniq, GroupBy, Must) and how to import specialized packages like parallel, mutable and iterator. It shows how lo wraps common patterns—mapping, filtering, grouping, uniqueness, error handling, and retries—into generic, readable calls. It also highlights where to fetch the up-to-date function list with a single curl command and cautions about unreleased changes.

When to use it

  • Replace repetitive for-loops with concise, generic Map/Filter/Reduce calls for slice processing.
  • Deduplicate or group collections while preserving order (Uniq, GroupBy) instead of custom bookkeeping.
  • Simplify error handling during initialization with Must and short-circuit helpers.
  • Implement retries, throttling, or debounce flows with built-in utilities instead of custom timers.
  • Write concurrent channel/parallel operations using the parallel helpers to reduce boilerplate.

Best practices

  • Import only what you need (lo, lo/parallel, lo/mutable, lo/it) to keep intent clear and reduce binary size.
  • Prefer lo helpers for readability, but benchmark hot paths—manual loops can be faster in tight, performance-critical code.
  • Use lo.Must for initialization where panic on error is acceptable; avoid Must in long-running request handlers.
  • Verify the installed lo version against the official function list; the curl endpoint may show unreleased changes.
  • Keep transformations pure and side-effect free when using Map/Filter to avoid hidden state bugs.

Example use cases

  • Transform and filter API payloads: lo.Map to project fields, lo.Filter to drop invalid items before validation.
  • Aggregate data: lo.GroupBy to bucket users by age then compute stats per bucket.
  • Clean up slices: lo.Uniq to remove duplicates and preserve insertion order before persisting.
  • Init-time parsing: lo.Must(time.Parse(...)) for app boot configuration where failure should crash fast.
  • Concurrent processing: use lop (parallel) helpers to run independent tasks across goroutines with built-in error aggregation.

FAQ

How do I get the authoritative list of lo functions?

Run curl -sSL https://lo.samber.dev/llms.txt to fetch the latest function list; confirm against your module version since the list may include unreleased changes.

Is lo safe for performance-critical loops?

lo improves readability but may allocate more than hand-written loops. Benchmark and profile hot paths before replacing optimized loops.