go-control-structures

Enforce Go control structure rules for range loops, breaks, and defer.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Guides Go developers to avoid common control-flow mistakes in loops, ensuring idiomatic and reliable code when iterating with range, using break inside nested constructs, or placing defer in loops.

Core Features & Use Cases

  • Enforces correct range loop mutations by using index access when mutating elements.
  • Flags incorrect break semantics inside switches/selections nested in loops and promotes labeled breaks.
  • Warns against defer statements inside loops and advocates extracting per-iteration cleanup into helper functions or closures.

Quick Start

Run a Go code review to ensure range loops mutate elements via index, avoid defer inside loops, and apply labeled breaks where appropriate.

Frequently Asked Questions about go-control-structures

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

FAQPage Schema
Why does my Go range loop not mutate slice elements?

Go range loops copy slice elements by value, preventing direct mutation. To correctly mutate elements in Go range loops, use index access to update the underlying slice or array directly during iteration.

How do I break out of a switch statement nested inside a Go loop?

A standard break inside a nested switch only exits the switch, not the outer Go loop. Use labeled breaks to correctly control flow and exit both the switch selection and the surrounding loop simultaneously.

What happens when you put defer inside a Go loop?

Placing defer inside a Go loop defers execution until the function ends, causing resource accumulation. Extract per-iteration cleanup into helper functions or closures to ensure resources are released immediately.

Can I use static analysis to check for incorrect defer in loops?

Yes, static analysis checks during Go code reviews can flag defer statements placed inside loops. This enforces proper defer handling by advocating for extraction of per-iteration cleanup into helper functions.

When do I need labeled breaks in Go control flow?

Labeled breaks are needed in Go control flow when you must exit nested constructs, such as breaking out of a switch or select statement that is contained within an outer for loop.