go-control-flow
/go-control-flow
Pomaga pisać warunki, pętle i switche w Go, w tym if z inicjalizacją, wczesne zwroty, formy pętli for, range, type switche i identyfikator pusty.
- Autor
- cxuu ↗
- Repozytorium
- golang-skills ↗
- Licencja
- Apache-2.0
- Ostatnia zmiana repozytorium
- 19.06.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 3
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 19.06.2026, bez odwołań do wycofanych modeli i API.
Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.
Co robi i kiedy
Pomaga pisać warunki, pętle i switche w Go, w tym if z inicjalizacją, wczesne zwroty, formy pętli for, range, type switche i identyfikator pusty.
Oryginalny opis (EN)
Use when writing conditionals, loops, or switch statements in Go — including if with initialization, early returns, for loop forms, range, switch, type switches, and blank identifier patterns. Also use when writing a simple if/else or for loop, even if the user doesn't mention guard clauses or variable scoping. Does not cover error flow patterns (see go-error-handling).
Jak działa
- Resource Routing
- If with Initialization
- Indent Error Flow (Guard Clauses)
- Redeclaration and Reassignment
- For Loops
- Switch: Labeled Break
- The Blank Identifier
- Quick Reference
- Related Skills
Podgląd SKILL.md
---
name: go-control-flow
description: Use when writing conditionals, loops, or switch statements in Go — including if with initialization, early returns, for loop forms, range, switch, type switches, and blank identifier patterns. Also use when writing a simple if/else or for loop, even if the user doesn't mention guard clauses or variable scoping. Does not cover error flow patterns (see go-error-handling).
---
# Go Control Flow
## Resource Routing
- `references/SWITCH-PATTERNS.md` - Read when using switch statements, type switches, fallthrough, or labeled breaks.
- `references/BLANK-IDENTIFIER.md` - Read when using `_`, blank imports, unused compile-time assertions, or intentional discards.
---
## If with Initialization
`if` and `switch` accept an optional initialization statement. Use it to scope
variables to the conditional block:
```go
if err := file.Chmod(0664); err != nil {
log.Print(err)
return err
}
```
If you need the variable beyond a few lines after the `if`, declare it
separately and use a standard `if` instead:
```go
x, err := f()
if err != nil {
return err
}
// lots of code that uses x
```
## Indent Error Flow (Guard Clauses)
When an `if` body ends with `break`, `continue`, `goto`, or `return`, omit the
unnecessary `else`. Keep the success path unindented:
```go
f, err := os.Open(name)
if err != nil {
return err
}
d, err := f.Stat()
if err != nil {
f.Close()
return err
}
codeUsing(f, d)
```
Never bury normal flow inside an `else` when the `if` already returns.
---
## Redeclaration and Reassignment
The `:=` short declaration allows redeclaring variables in the same scope:
```go
f, err := os.Open(name) // declares f and err
d, err := f.Stat() // declares d, reassigns err
```
A variable `v` may appear in a `:=` declaration even if already declared,
provided:
1. The declaration is in the **same scope** as the existing `v`
2. The value is **assignable** to `v`
3. At least **one other variable** is newly created by the declaration
### Variable Shadowing
**Warning**: If `v` is declared in an outer scope, `:=` creates a **new**
variable that shadows it — a common source of bugs:
```go
// Bug: ctx inside the if block shadows the outer ctx
if *shortenDeadlines {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
}
// ctx here is still the original — the shadowed ctx didn't escape
// Fix: use = instead of :=
var cancel func()
ctx, cancel = context.WithTimeout(ctx, 3*time.Second)
```
---
## For Loops
Go's `for` is its only looping construct, unifying `while`, `do-while`, and
C-style `for`:
```go
// Condition-only (Go's "while")
for x > 0 {
x = process(x)
}
// Infinite loop
for {
if done() { break }
}
// C-style three-component
for i := 0; i < n; i++ { ... }
```
### Range
`range` iterates over slices, maps, strings, and channels:
```go
for i, v := range slice { ... } // index + value
for k, v := range myMap { ... } // key + value (non-deterministic order)
for i, r := range "héllo" { ... } // byte index + rune (not byte)
for v := range ch { ... } // receives until channel closed
```
**Key rules:**
- Range over strings yields **runes**, not bytes — `i` is the byte offset
- Range over maps has **non-deterministic order** — don't rely on it
- Use `_` to discard the index or value: `for _, v := range slice`
### Parallel Assignment
Go has no comma operator. Use parallel assignment for multiple loop variables:
```go
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
```
`++` and `--` are statements, not expressions — they cannot appear in parallel
assignment.
---
## Switch: Labeled Break
`break` inside a `switch` within a `for` loop only breaks the switch.
Use a labeled `break` to exit the enclosing loop:
```go
Loop:
for _, v := range items {
switch v.Type {
case "done":
break Loop // breaks the for loop
}
}
```
For type switches, see **go-interfaces**: Type Switch.
---
## The Blank Identifier
**Never discard errors carelessly** — a nil dereference panic may follow.
Route compile-time interface assertions to [go-interfaces](../go-interfaces/SKILL.md).
---
## Quick Reference
| Pattern | Go Idiom |
|---------|----------|
| If initialization | `if err := f(); err != nil { }` |
| Early return | Omit `else` when if body returns |
| Redeclaration | `:=` reassigns if same scope + new var |
| Shadowing trap | `:=` in inner scope creates new variable |
| Parallel assignment | `i, j = i+1, j-1` |
| Expression-less switch | `switch { case cond: }` |
| Comma cases | `case 'a', 'b', 'c':` |
| No fallthrough | Default behavior (explicit `fallthrough` if needed) |
| Break from loop in switch | `break Label` |
| Discard value | `_, err := f()` |
| Side-effect import | `import _ "pkg"` |
| Interface check | Route to go-interfaces |
---
## Related Skills
- **Error flow**: See [go-error-handling](../go-error-handling/SKILL.md) when structuring guard clauses, early returns, or error-first patterns
- **Type switches**: See [go-interfaces](../go-interfaces/SKILL.md) when using type switches, the comma-ok idiom, or interface satisfaction checks
- **Nesting reduction**: See [go-style-core](../go-style-core/SKILL.md) when reducing nesting depth or resolving formatting questions
- **Variable scoping**: See [go-declarations](../go-declarations/SKILL.md) when using if-init, `:=` redeclaration, or reducing variable scope
Pliki w paczce (3)
- SKILL.md 5 KB
- references/BLANK-IDENTIFIER.md 2 KB
- references/SWITCH-PATTERNS.md 2 KB
Wersje
| Repozytorium | Wynik | Licencja |
|---|---|---|
| Ta strona · cxuu/golang-skills | 100 | Apache-2.0 |
| muratmirgun/gophers | 100 | MIT |
| muratmirgun/gophers | 100 | MIT |
Migawka bazy z 28.10.2026 · wersja paczki e7c499592542 (skrót SHA-256 całej paczki)
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100kotlin-control-flow
Pomaga pisać i przeglądać rozgałęzienia w Kotlinie: wyrażenia when, warunki guard, kompletność typów sealed, smart casty, rozgałęzienia na wartościach nullable i wczesne zwroty zamiast złożonych if else. - 100go-control-structures
Pilnuje poprawnego użycia struktur sterujących w Go: pętli range, break w switch i select wewnątrz pętli oraz defer w pętlach. - 100go-code-style
Pilnuje stylu kodu Go: kolejność priorytetów (przejrzystość, prostota, zwięzłość, utrzymywalność, spójność), reguły gofmt, wczesne zwroty bez else, switch zamiast łańcuchów if, inicjalizacja slice i map oraz wartość kontra wskaźnik. - 100go-code-style
Pilnuje stylu kodu Go: kolejność priorytetów (przejrzystość, prostota, zwięzłość, utrzymywalność, spójność), reguły gofmt, wczesne zwroty bez else, switch zamiast łańcuchów if, inicjalizacja slice i map oraz wartość kontra wskaźnik. - 100control-flow
Prostuje skomplikowany przepływ sterowania przez wczesne zwroty, klauzule ochronne i spłaszczanie zagnieżdżonych warunków oraz bloków try/catch. - 100handle-errors
Stosuje wzorce obsługi błędów w bazie kodu: walidację Zod na granicach, typowane błędy, wczesne zwroty oraz retry z backoffem.