Skip to main content
skill CX Codex

go-style-core — Claude Code skill: core Go style rules

A skill that lays out core Go style rules: formatting, nesting, naked returns, semicolons. Useful whenever the agent writes or edits Go code.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add cxuu/golang-skills --skill go-style-core -a claude-code
Open on GitHub

What go-style-core does

The skill ranks five principles by importance: clarity, simplicity, concision, maintainability, consistency. When two of them pull in opposite directions, that order settles the argument — the details live in references/PRINCIPLES.md.

Two reference files ship with it. Reach for references/FORMATTING.md on questions about gofmt, line breaks, whitespace, comments or semicolons. There is no rigid line length limit, though Uber suggests a soft one of 99 characters; breaks should follow semantics rather than a character count, and refactoring beats wrapping a long line.

Nesting and naked returns get their own sections. Error cases and special conditions go first so the happy path stays unindented. A bare return is fine in a short function; once it grows past a handful of lines, write the return values out. Error handling, naming and testing are deliberately out of scope — separate skills in the same set cover those.

What it helps with

  • flatten deeply nested conditionals with early returns
  • decide whether a naked return fits a given function
  • work out where the opening brace goes and when a semicolon is needed
  • pick between clarity and concision when they conflict
  • bring line breaks and line length closer to gofmt output

Example prompts for the agent

  • clean up this function with go-style-core
  • this nesting is too deep — rewrite it following Go style rules
  • check the naked returns in this file and tell me where explicit values are clearer

How to install go-style-core

1Skills CLI (Vercel)

npx skills add cxuu/golang-skills --skill go-style-core -a claude-code

2Manually: put the files in place

~/.claude/skills/go-style-core/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/go-style-core/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about go-style-core

How do I install go-style-core in Claude?

Run npx skills add cxuu/golang-skills --skill go-style-core -a claude-code. The skill lands in ~/.claude/skills/go-style-core/SKILL.md.

What does go-style-core do?

It sets out core Go style rules: principle priority, formatting, reduced nesting, naked returns and semicolons. It also acts as a fallback when no more specific style skill applies.

What does it not cover?

Error handling, naming and testing — the same set has dedicated skills for those. go-style-core points to them when a question goes beyond core style.

Source text

SKILL.md · Apache-2.0
---
name: go-style-core
description: Use when working with Go formatting, line length, nesting, naked returns, semicolons, or core style principles. Also use when a style question isn't covered by a more specific skill, even if the user doesn't reference a specific style rule. Does not cover domain-specific patterns like error handling, naming, or testing (see specialized skills). Acts as fallback when no more specific style skill applies.
---

# Go Style Core Principles

## Resource Routing

- `references/PRINCIPLES.md` - Read when resolving conflicts between clarity, simplicity, concision, maintainability, and consistency.
- `references/FORMATTING.md` - Read when handling gofmt, line breaks, whitespace, comments, or semicolons.

## Style Principles (Priority Order)

When writing readable Go code, apply these principles in order of importance:

### Priority Order

1. **Clarity** — Can a reader understand the code without extra context?
2. **Simplicity** — Is this the simplest way to accomplish the goal?
3. **Concision** — Does every line earn its place?
4. **Maintainability** — Will this be easy to modify later?
5. **Consistency** — Does it match surrounding code and project conventions?

---

## Formatting

Run `gofmt` — no exceptions. There is **no rigid line length limit**, but Uber suggests a soft limit of 99 characters. Break by semantics, not length — refactor rather than just wrap.

---

## Reduce Nesting

Handle error cases and special conditions first. Return early or continue the loop to keep the "happy path" unindented.

```go
// Bad: Deeply nested
for _, v := range data {
    if v.F1 == 1 {
        v = process(v)
        if err := v.Call(); err == nil {
            v.Send()
        } else {
            return err
        }
    } else {
        log.Printf("Invalid v: %v", v)
    }
}

// Good: Flat structure with early returns
for _, v := range data {
    if v.F1 != 1 {
        log.Printf("Invalid v: %v", v)
        continue
    }

    v = process(v)
    if err := v.Call(); err != nil {
        return err
    }
    v.Send()
}
```

### Unnecessary Else

If a variable is set in both branches of an if, use default + override pattern.

```go
// Bad: Setting in both branches
var a int
if b {
    a = 100
} else {
    a = 10
}

// Good: Default + override
a := 10
if b {
    a = 100
}
```

---

## Naked Returns

A `return` statement without arguments returns the named return values. This is
known as a "naked" return.

```go
func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // returns x, y
}
```

### Guidelines for Naked Returns

- **OK in small functions**: Naked returns are fine in functions that are just a
  handful of lines
- **Be explicit in medium+ functions**: Once a function grows to medium size, be
  explicit with return values for clarity
- **Don't name results just for naked returns**: Clarity of documentation is
  always more important than saving a line or two

```go
// Good: Small function, naked return is clear
func minMax(a, b int) (min, max int) {
    if a < b {
        min, max = a, b
    } else {
        min, max = b, a
    }
    return
}

// Good: Larger function, explicit return
func processData(data []byte) (result []byte, err error) {
    result = make([]byte, 0, len(data))

    for _, b := range data {
        if b == 0 {
            return nil, errors.New("null byte in data")
        }
        result = append(result, transform(b))
    }

    return result, nil // explicit: clearer in longer functions
}
```

See **go-documentation** for guidance on Named Result Parameters.

---

## Semicolons

Go's lexer automatically inserts semicolons after any line whose last token is
an identifier, literal, or one of: `break continue fallthrough return ++ -- ) }`.

This means **opening braces must be on the same line** as the control structure:

```go
// Good: brace on same line
if i < f() {
    g()
}

// Bad: brace on next line — lexer inserts semicolon after f()
if i < f()  // wrong!
{           // wrong!
    g()
}
```

Idiomatic Go only has explicit semicolons in `for` loop clauses and to separate
multiple statements on a single line.

---

## Quick Reference

| Principle | Key Question |
|-----------|--------------|
| Clarity | Can a reader understand what and why? |
| Simplicity | Is this the simplest approach? |
| Concision | Is the signal-to-noise ratio high? |
| Maintainability | Can this be safely modified later? |
| Consistency | Does this match surrounding code? |

## Related Skills

- **Naming conventions**: See [go-naming](../go-naming/SKILL.md) when applying MixedCaps, choosing identifier names, or resolving naming debates
- **Error flow**: See [go-error-handling](../go-error-handling/SKILL.md) when structuring error-first guard clauses or reducing nesting via early returns
- **Documentation**: See [go-documentation](../go-documentation/SKILL.md) when writing doc comments, named return parameters, or package-level docs
- **Linting enforcement**: See [go-linting](../go-linting/SKILL.md) when automating style checks with golangci-lint or configuring CI
- **Code review**: See [go-code-review](../go-code-review/SKILL.md) when applying style principles during a systematic code review
- **Logging style**: See [go-logging](../go-logging/SKILL.md) when reviewing logging practices, choosing between log and slog, or structuring log output

© cxuu · Apache-2.0 · cxuu/golang-skills

Similar skills

All skills for Programming languages →

From the same set19

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor