All skills
cxuu avatar

/go-declarations

@91f0c2e
by Charles Xucxuu/golang-skills165 stars
19

Use when declaring or initializing Go variables, constants, structs, or maps — including var vs :=, reducing scope with if-init, formatting composite literals, designing iota enums, and using any instead of interface{}. Also use when writing a new struct or const block, even if the user doesn't ask about declaration style. Does not cover naming conventions (see go-naming).

Use this Skill: https://skilld.dev/gh/cxuu/golang-skills/go-declarations

This session only. Nothing lands on disk.

referencesIOTA.md

≈487 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Constants and iota Patterns

Source: Uber Style Guide, Google Style Guide

Detailed patterns for designing enumerated constants with iota in Go.


Start Enums at One

Start enums at one so the zero value represents an invalid/unset state. This catches uninitialized variables:

type Operation int

const (
    Add Operation = iota + 1
    Subtract
    Multiply
)
// Add=1, Subtract=2, Multiply=3

When Zero Makes Sense

Use zero when the default behavior is desirable:

type LogOutput int

const (
    LogToStdout LogOutput = iota  // zero value = default
    LogToFile
    LogToRemote
)

The key question: is the zero value a valid, useful default? If yes, start at zero. If no, start at one.


Bitmask Patterns

Use bit-shifting with iota for flag/bitmask enums:

type Permission int

const (
    Read    Permission = 1 << iota  // 1
    Write                           // 2
    Execute                         // 4
)

// Combine with bitwise OR
perms := Read | Write  // 3

Byte Size Pattern

A common pattern for byte size constants:

type ByteSize float64

const (
    _           = iota // ignore first value (0)
    KB ByteSize = 1 << (10 * iota)
    MB
    GB
    TB
    PB
)

String Representation

Always implement String() for enum types to aid debugging:

func (o Operation) String() string {
    switch o {
    case Add:
        return "Add"
    case Subtract:
        return "Subtract"
    case Multiply:
        return "Multiply"
    default:
        return fmt.Sprintf("Operation(%d)", o)
    }
}

Consider using go generate with stringer for automatic string methods on large enums.


Grouping Rules

  • Each enum type gets its own const block — iota resets to 0 in each block
  • Unrelated constants go in separate blocks
  • Document the enum type, not each individual constant (unless behavior is non-obvious)

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The analyzed skill contains only markdown guidelines and formatting rules for Go programming declarations. No code execution, unsafe tools, obfuscated elements, or risk factors were found.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    2 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 91f0c2e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago

README badge

README badge for cxuu/golang-skills/go-declarations