AGENT SKILL

go-linq

Use github.com/ahmetb/go-linq/v5 for LINQ-style queries in Go 1.27+. Provides fully type-safe, generic-method-based query chains over slices, maps, channels, strings, and iter.Seq[T].

License 许可
MIT
最近更新
2026-10-30
Stars
★ 2
Forks
2

01 前言 Front Matter

name
go-linq
license
MIT
metadata
{ "source": "https://github.com/ahmetb/go-linq", "version": "1.0" }
description
Use github.com/ahmetb/go-linq/v5 for LINQ-style queries in Go 1.27+. Provides fully type-safe, generic-method-based query chains over slices, maps, channels, strings, and iter.Seq[T].
compatibility
Go 1.27+ (generic methods required). Intended for Go codebases using github.com/ahmetb/go-linq/v5.

03 SKILL.md 正文

Skill: go-linq v5 (github.com/ahmetb/go-linq/v5)

Purpose

Provide LINQ-style query capabilities in Go using fully type-safe, generic-method-based APIs introduced in Go 1.27.
Use this skill whenever you need composable, lazy, type-safe queries over slices, maps, channels, strings, or iter.Seq[T].

When to Use

  • Go version: 1.27+ (required for generic methods).
  • Requirement: LINQ-like query chains with no interface{}/any, no type assertions, and minimal allocations.
  • Import path: github.com/ahmetb/go-linq/v5.

If Go < 1.27 or generic methods are unavailable, fall back to github.com/ahmetb/go-linq/v4 (any-based API).

Installation

go get github.com/ahmetb/go-linq/v5

In go.mod:

require github.com/ahmetb/go-linq/v5 v5.0.0

Core Concepts

  • Query[T] is the central type; all operators preserve or transform T in a type-safe way.
  • Type-changing operators (e.g. Select, SelectMany, Join, GroupBy) are generic methods whose result type is inferred from your functions.
  • No reflection-based …T variants: base methods are already typed.
  • Terminals like First, Last, Single, Min, Max return (T, bool) instead of any.
  • ToSlice() returns []T directly (no pointer argument).
  • Data sources use typed constructors: FromSlice, FromMap, FromChannel, FromString, FromSeq, Range, Repeat.

Usage Examples

Example 1: Simple filter + projection (slice of structs)

import . "github.com/ahmetb/go-linq/v5"

type Car struct {
    Year  int
    Owner string
    Model string
}

func ownersAfter2015(cars []Car) []string {
    return FromSlice(cars).
        Where(func(c Car) bool { return c.Year >= 2015 }).
        Select(func(c Car) string { return c.Owner }).
        ToSlice()
}

Example 2: Find author with most books (grouping + aggregation)

import . "github.com/ahmetb/go-linq/v5"

type Book struct {
    ID      int
    Title   string
    Authors []string
}

func topAuthor(books []Book) (author string, ok bool) {
    g, ok := FromSlice(books).
        SelectMany(func(b Book) Query[string] {
            return FromSlice(b.Authors)
        }).
        GroupBy(
            func(a string) string { return a },
            func(a string) string { return a },
        ).
        MaxBy(func(g Group[string, string]) int { return len(g.Group) })

    if !ok {
        return "", false
    }
    return g.Key, true
}

Example 3: MapReduce-style word frequency (top 5 words)

import . "github.com/ahmetb/go-linq/v5"

func top5Words(sentences []string) []string {
    return FromSlice(sentences).
        SelectMany(func(s string) Query[string] {
            return FromSlice(strings.Split(s, " "))
        }).
        GroupBy(
            func(w string) string { return w },
            func(w string) string { return w },
        ).
        OrderByDescending(func(g Group[string, string]) int { return len(g.Group) }).
        ThenBy(func(g Group[string, string]) string { return g.Key }).
        Take(5).
        SelectIndexed(func(i int, g Group[string, string]) string {
            return fmt.Sprintf("Rank: #%d, Word: %s, Count: %d", i+1, g.Key, len(g.Group))
        }).
        ToSlice()
}

Example 4: Custom extension method

import . "github.com/ahmetb/go-linq/v5"

type MyQuery Query[int]

func (q MyQuery) GreaterThan(threshold int) Query[int] {
    return Query[int](q).Where(func(v int) bool { return v > threshold })
}

func gt5() []int {
    return MyQuery(Range(1, 10)).GreaterThan(5).ToSlice()
}

Example 5: Iteration with standard for range

import . "github.com/ahmetb/go-linq/v5"

q := FromSlice([]int{1, 2, 3, 4})

for v := range q.Iterate {
    fmt.Println(v)
}

Common Patterns

  • Filtering + projection: FromSlice(xs).Where(...).Select(...).ToSlice()
  • Flattening: SelectMany to turn []T into Query[U].
  • Grouping: GroupBy(keyFunc, valueFunc) → Query[Group[K, V]].
  • Aggregation: use MaxBy, MinBy, SumBy, AverageBy for chainable aggregations; or package-level Max, Min, Sum, etc.
  • Ordering: OrderBy / OrderByDescending / ThenBy with cmp.Ordered keys; use Sort for custom comparators.

Migration Notes (v4 → v5)

  • Replace Query with Query[T] implicitly via typed constructors (FromSlice, etc.).
  • Remove all …T methods (WhereT, SelectT, …); use Where, Select, etc. directly.
  • Replace From(any) with FromSlice, FromMap, FromChannel, FromString, FromSeq.
  • Adjust terminals: First(), Last(), etc. now return (T, bool).
  • ToSlice() now returns []T; remove the &slice argument pattern.

Performance Characteristics

  • 5–15× faster than v4 any-based API.
  • Allocations drop from O(n) per query to O(1) per query (only fixed closure captures).
  • Ideal for high-throughput pipelines over large slices/iterators.

References

04 同仓库更多 Skills

dhcgn
dhcgn/immich-admin-cli

immich-admin-cli

暂无简介

★ 2
dhcgn
dhcgn/immich-admin-cli

wsl

Windows Subsystem for Linux (WSL) allows you to run a Linux environment directly on Windows, unmodified, without the overhead of a traditional virtual machine. If you need to run Linux commands, scripts, or applications on a Windows machine, WSL is the way to go. This skill provides guidance on how to use WSL effectively.

★ 2
dhcgn
dhcgn/immich-admin-cli

rtk

Prefix covered shell commands with rtk so output is filtered/compressed before it reaches LLM context — git, gh, cargo, pytest, go test, docker, kubectl, aws, ls, grep, and more. Applies whenever a covered command is about to be executed in a shell.

★ 2
dhcgn
dhcgn/immich-admin-cli

ponytail

Forces the laziest solution that actually works, simplest, shortest, most minimal. Channels a senior dev who has seen everything: question whether the task needs to exist at all (YAGNI), reach for the standard library before custom code, native platform features before dependencies, one line before fifty. Supports intensity levels: lite, full (default), ultra. Use on ANY coding task: writing, adding, refactoring, fixing, reviewing, or designing code, and choosing libraries or dependencies. Also use whenever the user says "ponytail", "be lazy", "lazy mode", "simplest solution", "minimal solution", "yagni", "do less", or "shortest path", or complains about over-engineering, bloat, boilerplate, or unnecessary dependencies. Do NOT use for non-coding requests (general knowledge, prose, translation, summaries, recipes).

★ 2
dhcgn
dhcgn/immich-admin-cli

ponytail-review

Code review focused exclusively on over-engineering. Finds what to delete: reinvented standard library, unneeded dependencies, speculative abstractions, dead flexibility. One line per finding: location, what to cut, what replaces it. Use when the user says "review for over-engineering", "what can we delete", "is this over-engineered", "simplify review", or invokes /ponytail-review. Complements correctness-focused review, this one only hunts complexity.

★ 2
dhcgn
dhcgn/immich-admin-cli

ponytail-help

Quick-reference card for all ponytail modes, skills, and commands. One-shot display, not a persistent mode. Trigger: /ponytail-help, "ponytail help", "what ponytail commands", "how do I use ponytail".

★ 2