go-strings

Optimize Go string handling with rune-aware iteration, builder concatenation, and safe substring usage.

15|Updated Mar 4, 2026
One-click install
npx skills add https://github.com/v0lka/skills --skill go-strings
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-strings
Source: https://github.com/v0lka/skills/tree/main/development/idiomatic-go/go-strings
Command: npx skills add https://github.com/v0lka/skills --skill go-strings

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go developers often write inefficient or error-prone string handling routines. This skill provides guidance to write correct and efficient Go string code, covering rune semantics, iteration, trimming, conversion between string and []byte, and safe substring usage to avoid memory leaks.

Core Features & Use Cases

  • Rune-aware string iteration: correctly range over strings to access runes rather than bytes.
  • Efficient concatenation: prefer strings.Builder with pre-growth when total size is known.
  • Substrings safety: avoid keeping large backing arrays by cloning substrings when storing them.
  • Trim vs trim functions: choose TrimPrefix/TrimSuffix for fixed strings and TrimLeft/TrimRight/Trim for cutsets.
  • Use-case example: refactor a function that concatenates 100 short strings into a single string efficiently without excessive allocations.

Quick Start

Refactor a Go function to use strings.Builder for efficient concatenation of a given slice of strings.

Frequently Asked Questions about go-strings

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does iterating over a Go string with a standard loop return bytes instead of characters?

Iterating over a Go string with a standard loop returns bytes because strings are immutable byte sequences, but ranging over a string decodes UTF-8 into runes, yielding the correct character indices for safe text processing.

What's the best way to concatenate a large slice of strings in Go without excessive allocations?

The best way to concatenate strings in Go without excessive allocations is using strings.Builder with pre-growth when the total size is known. This minimizes memory copying and optimizes overall concatenation performance.

How do I prevent memory leaks when extracting substrings from a large Go string?

To prevent memory leaks when extracting substrings from a large Go string, clone the substring using strings.Clone or by converting it to a byte slice and back. This avoids keeping the large backing array alive in memory.

When should I use TrimPrefix versus TrimLeft for string manipulation in Go?

Use TrimPrefix or TrimSuffix when removing a specific fixed string from the start or end, and use TrimLeft, TrimRight, or Trim when removing characters matching a cutset. Choosing the correct trim variant ensures accurate string manipulation.

Does converting between a Go string and a byte slice cause a memory allocation?

Converting between a Go string and a byte slice causes a memory allocation because it requires copying the underlying data to ensure immutability. Optimizing these conversions is crucial for high-performance string handling.