跳到主内容
智客 ZICQ

技能库 智客分类:Agent 工作流 golang-data-structures

Golang 数据结构

戈兰格数据结构——切片(内部,容量增长,预分配,切片包),地图(内部,散列桶,地图包),阵列,容器/列表/堆放/环,字符串. 构建器 vs 字节 。 缓冲,通用集合,指针(不安全. 指取而为弱. 指针),并抄取语义. 选择或优化Go数据结构时使用,执行通用容器,使用容器/包,不安全或弱指针,或质疑切片/图内部. 一旦特征分析发现存在瓶颈,则不适用优化模式(-见`Samber/cc-shilling-golang@golang-performance' 技能).

38710 安装量

官方网址:作者主页

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

戈兰格数据结构——切片(内部,容量增长,预分配,切片包),地图(内部,散列桶,地图包),阵列,容器/列表/堆放/环,字符串. 构建器 vs 字节 。 缓冲,通用集合,指针(不安全. 指取而为弱. 指针),并复制语义

何时用

每种容器类型、增加类型安全的通用包装和高效I/O " bufio " 型号

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Go Data Structures、Best Practices Summary、Slice Internals、Capacity Growth、Preallocation、`slices` Package (Go 1.21+)。

文件分析

文件分析:除 SKILL.md 外,正文引用了 references/slice-internals.md、references/map-internals.md、references/containers.md、references/generics.md、references/pointers.md,属于带资源的技能包,这些文件按需再读。

官方 description(原文)

Golang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals. Not for applying optimization patterns once profiling has identified a bottleneck (→ See `samber/cc-skills-golang@golang-performance` skill).

Go Data StructuresBest Practices SummarySlice InternalsCapacity GrowthPreallocation`slices` Package (Go 1.21+)Map InternalsPreallocation`maps` Package Quick Reference (Go 1.21+)Arrayscontainer/ Standard Librarystrings.Builder vs bytes.Buffer

兼容:Designed for Claude Code, Codex or similar harness, and for projects using Golang. · 许可:MIT · allowed-tools:Read Edit Write Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) Agent Bash(godig:*) Bash(gopls:*) LSP mcp__gopls__* mcp__context7__resolve-library-id mcp__context7__query-docs

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
golang-data-structures
description
Golang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals. Not for applying optimization patterns once profiling has identified a bottleneck (→ See `samber/cc-skills-golang@golang-performance` skill).
compatibility
Designed for Claude Code, Codex or similar harness, and for projects using Golang.
allowed-tools
Read Edit Write Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) Agent Bash(godig:*) Bash(gopls:*) LSP mcp__gopls__* mcp__context7__resolve-library-id mcp__context7__query-docs实验字段,支持情况取决于客户端;字段声明本身不会授予工具权限。
许可
MIT
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。
指令中引用的文件 · 5
  • references/slice-internals.md
  • references/map-internals.md
  • references/containers.md
  • references/generics.md
  • references/pointers.md

以下路径提取自原文;文件是否齐全请以来源仓库中的完整目录为准。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

该技能引用了附属文件,请从来源获取完整目录;仅复制 SKILL.md 可能缺少依赖。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「golang-data-structures」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-cda1773e5e438d81-Golang-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html
请存为 .cursor/skills/golang-data-structures/SKILL.md 或 .claude/skills/golang-data-structures/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。
该技能还带 scripts/、references/、assets/ 等文件,请从 https://github.com/samber/cc-skills-golang 取完整目录,不要只建一个 SKILL.md。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/samber/cc-skills-golang' --list

npx skills add 'https://github.com/samber/cc-skills-golang' --skill 'golang-data-structures'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版

name: golang-data-structures description: "Golang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals. Not for applying optimization patterns once profiling has identified a bottleneck (→ See samber/cc-skills-golang@golang-performance skill)." user-invocable: true license: MIT compatibility: Designed for Claude Code, Codex or similar harness, and for projects using Golang. metadata: author: samber version: "1.2.2" openclaw: emoji: "🗃" homepage: https://github.com/samber/cc-skills-golang requires: bins: - go install: [] allowed-tools: Read Edit Write Glob Grep Bash(go:) Bash(golangci-lint:) Bash(git:) Agent Bash(godig:) Bash(gopls:) LSP mcp__gopls__ mcp__context7__resolve-library-id mcp__context7__query-docs paths:

  • "**/*.go"

Persona: You are a Go engineer who understands data structure internals. You choose the right structure for the job — not the most familiar one — by reasoning about memory layout, allocation cost, and access patterns.

Go Data Structures

Built-in and standard library data structures: internals, correct usage, and selection guidance.

  • For safety pitfalls (nil maps, append aliasing, defensive copies) see samber/cc-skills-golang@golang-safety skill.
  • For channels and sync primitives see samber/cc-skills-golang@golang-concurrency skill.
  • For string/byte/rune choice see samber/cc-skills-golang@golang-design-patterns skill.

Best Practices Summary

  1. Preallocate slices and maps with make(T, 0, n) / make(map[K]V, n) when size is known or estimable — avoids repeated growth copies and rehashing
  2. Arrays SHOULD be preferred over slices only for fixed, compile-time-known sizes (hash digests, IPv4 addresses, matrix dimensions)
  3. NEVER rely on slice capacity growth timing — the growth algorithm changed between Go versions and may change again; your code should not depend on when a new backing array is allocated
  4. Use container/heap for priority queues, container/list only when frequent middle insertions are needed, container/ring for fixed-size circular buffers
  5. strings.Builder MUST be preferred for building strings; bytes.Buffer MUST be preferred for bidirectional I/O (implements both io.Reader and io.Writer)
  6. Generic data structures SHOULD use the tightest constraint possible — comparable for keys, custom interfaces for ordering
  7. unsafe.Pointer MUST only follow the 6 valid conversion patterns from the Go spec — NEVER store in a uintptr variable across statements
  8. weak.Pointer[T] (Go 1.24+) SHOULD be used for caches and canonicalization maps to allow GC to reclaim entries

Slice Internals

A slice is a 3-word header: pointer, length, capacity. Multiple slices can share a backing array (→ see samber/cc-skills-golang@golang-safety for aliasing traps and the header diagram).

Capacity Growth

  • < 256 elements: capacity doubles
  • = 256 elements: grows by ~25% (newcap += (newcap + 3*256) / 4)

  • Each growth copies the entire backing array — O(n)

Preallocation

// Exact size known
users := make([]User, 0, len(ids))

// Approximate size known
results := make([]Result, 0, estimatedCount)

// Pre-grow before bulk append (Go 1.21+)
s = slices.Grow(s, additionalNeeded)

slices Package (Go 1.21+)

Key functions: Sort/SortFunc, BinarySearch, Contains, Compact, Grow. For Clone, Equal, DeleteFunc → see samber/cc-skills-golang@golang-safety skill.

Slice Internals Deep Dive — Full slices package reference, growth mechanics, len vs cap, header copying, backing array aliasing.

Map Internals

Maps are hash tables with 8-entry buckets and overflow chains. They are reference types — assigning a map copies the pointer, not the data.

Preallocation

m := make(map[string]*User, len(users)) // avoids rehashing during population

maps Package Quick Reference (Go 1.21+)

| Function | Purpose | | ----------------- | ---------------------------- | | Collect (1.23+) | Build map from iterator | | Insert (1.23+) | Insert entries from iterator | | All (1.23+) | Iterator over all entries | | Keys, Values | Iterators over keys/values |

For Clone, Equal, sorted iteration → see samber/cc-skills-golang@golang-safety skill.

Map Internals Deep Dive — How Go maps store and hash data, bucket overflow chains, why maps never shrink (and what to do about it), comparing map performance to alternatives.

Arrays

Fixed-size, value types. Copied entirely on assignment. Use for compile-time-known sizes:

type Digest [32]byte           // fixed-size, value type
var grid [3][3]int             // multi-dimensional
cache := map[[2]int]Result{}   // arrays are comparable — usable as map keys

Prefer slices for everything else — arrays cannot grow and pass by value (expensive for large sizes).

container/ Standard Library

| Package | Data Structure | Best For | | --- | --- | --- | | container/list | Doubly-linked list | LRU caches, frequent middle insertion/removal | | container/heap | Min-heap (priority queue) | Top-K, scheduling, Dijkstra | | container/ring | Circular buffer | Rolling windows, round-robin | | bufio | Buffered reader/writer/scanner | Efficient I/O with small reads/writes |

Container types use any (no type safety) — consider generic wrappers. Container Patterns, bufio, and Examples — When to use each container type, generic wrappers to add type safety, and bufio patterns for efficient I/O.

strings.Builder vs bytes.Buffer

Use strings.Builder for pure string concatenation (avoids copy on String()), bytes.Buffer when you need io.Reader or byte manipulation. Both support Grow(n). Details and comparison

Generic Collections (Go 1.18+)

Use the tightest constraint possible. comparable for map keys, cmp.Ordered for sorting, custom interfaces for domain-specific ordering.

type Set[T comparable] map[T]struct{}

func (s Set[T]) Add(v T)          { s[v] = struct{}{} }
func (s Set[T]) Contains(v T) bool { _, ok := s[v]; return ok }

Writing Generic Data Structures — Using Go 1.18+ generics for type-safe containers, understanding constraint satisfaction, and building domain-specific generic types.

Pointer Types

| Type | Use Case | Zero Value | | --- | --- | --- | | *T | Normal indirection, mutation, optional values | nil | | unsafe.Pointer | FFI, low-level memory layout (6 spec patterns only) | nil | | weak.Pointer[T] (1.24+) | Caches, canonicalization, weak references | N/A |

Pointer Types Deep Dive — Normal pointers, unsafe.Pointer (the 6 valid spec patterns), and weak.Pointer[T] for GC-safe caches that don't prevent cleanup.

Copy Semantics Quick Reference

| Type | Copy Behavior | Independence | | --- | --- | --- | | int, float, bool, string | Value (deep copy) | Fully independent | | array, struct | Value (deep copy) | Fully independent | | slice | Header copied, backing array shared | Use slices.Clone | | map | Reference copied | Use maps.Clone | | channel | Reference copied | Same channel | | *T (pointer) | Address copied | Same underlying value | | interface | Value copied (type + value pair) | Depends on held type |

Third-Party Libraries

For advanced data structures (trees, sets, queues, stacks) beyond the standard library:

  • emirpasic/gods — comprehensive collection library (trees, sets, lists, stacks, maps, queues)
  • deckarep/golang-set — thread-safe and non-thread-safe set implementations
  • gammazero/deque — fast double-ended queue

When using third-party libraries, refer to their official documentation and code examples for current API signatures.

  • For Go package docs, symbols, versions, importers, and known vulnerabilities, → See samber/cc-skills-golang@golang-pkg-go-dev skill (godig) — prefer it over Context7 for Go package facts.
  • To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See samber/cc-skills-golang@golang-gopls skill (gopls).
  • Context7 remains a fallback for docs not indexed on pkg.go.dev.

Cross-References

  • → See samber/cc-skills-golang@golang-performance skill for struct field alignment, memory layout optimization, and cache locality
  • → See samber/cc-skills-golang@golang-safety skill for nil map/slice pitfalls, append aliasing, defensive copying, slices.Clone/Equal
  • → See samber/cc-skills-golang@golang-concurrency skill for channels, sync.Map, sync.Pool, and all sync primitives
  • → See samber/cc-skills-golang@golang-design-patterns skill for string vs []byte vs []rune, iterators, streaming
  • → See samber/cc-skills-golang@golang-structs-interfaces skill for struct composition, embedding, and generics vs any
  • → See samber/cc-skills-golang@golang-code-style skill for slice/map initialization style

Common Mistakes

| Mistake | Fix | | --- | --- | | Growing a slice in a loop without preallocation | Each growth copies the entire backing array — O(n) per growth. Use make([]T, 0, n) or slices.Grow | | Using container/list when a slice would suffice | Linked lists have poor cache locality (each node is a separate heap allocation). Benchmark first | | bytes.Buffer for pure string building | Buffer's String() copies the underlying bytes. strings.Builder avoids this copy | | unsafe.Pointer stored as uintptr across statements | GC can move the object between statements — the uintptr becomes a dangling reference | | Large struct values in maps (copying overhead) | Map access copies the entire value. Use map[K]*V for large value types to avoid the copy |

References

相关技能

Agent 工作流

技能创建者Skill Creator

创造有效技能指南。 当用户想创造出新的技能(或更新现有的技能),以专业知识,工作流程,或工具集成来扩展克洛德的能力时,应该使用这种技能.

Agent 工作流

克劳德胡布Clawdhub

使用ClawdHub CLI搜索,安装,更新并发布从taladhub.com的代理技能. 需要获取苍蝇上的新技能时使用,将安装的技能同步到最新版本或特定版本,或者发布 npm-instainddhub CLI 的新/更新的技能文件夹.

Agent 工作流

团队指挥Agent Team Orchestration

管弦乐团多代理团队,任务设定周期,交接协议,审查工作流程. 使用时间: (1)建立2+特派员队伍,具有不同专业,(2)确定任务路线和生命周期(收录框_ spec_建设_审查_完成),(3)在特派员之间制定交接协议,(4)建立审查和质量关口,(5)管理特派员之间的交流和文物共享.

Agent 工作流

超级力量Superpowers

Spec-first,TDD,子代理驱动的软件开发工作流程. 当:(1)构建任何新功能或应用——触发脑暴_计划_子代理执行回路,(2)调试出一个bug或测试失败——触发系统性的根起过程,(3)用户说"让我们构建","帮助我计划","我想添加X",或"这个被打破",(4)完成一个功…