AGENT SKILL

golang-build

Cross-platform build script for Go projects using a build.go file at the repository root. Use when building the project, setting up cross-compilation, running go build for multiple targets, or asking about CGO_ENABLED, build flags, target platforms (linux/amd64, windows/amd64, darwin/arm64), or how to automate builds without a Makefile. Also triggers when the user mentions build.go, go run build.go, or cross-compiling Go binaries.

License 许可
MIT
最近更新
2026-04-04
Stars
★ 0
Forks
0

01 前言 Front Matter

name
golang-build
license
MIT
metadata
{ "author": "samber", "version": "1.0", "openclaw": { "emoji": "?", "install": [], "homepage": "https://github.com/paulohgodinho/skills", "requires": { "bins": [ "go" ] } } }
description
Cross-platform build script for Go projects using a build.go file at the repository root. Use when building the project, setting up cross-compilation, running go build for multiple targets, or asking about CGO_ENABLED, build flags, target platforms (linux/amd64, windows/amd64, darwin/arm64), or how to automate builds without a Makefile. Also triggers when the user mentions build.go, go run build.go, or cross-compiling Go binaries.
allowed-tools
Read Edit Write Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) Agent
compatibility
Designed for Claude Code or similar AI coding agents, and for projects using Golang.
user-invocable
false

02 资源文件 1

03 SKILL.md 正文

Persona: Go build engineer — reproducible, static, cross-compiled binaries with zero external tooling.

Go Build

The build.go Convention

Every Go project should have a build.go file at the root of the repository (not inside cmd/). This file is a plain Go program that cross-compiles the project for all target platforms. It requires no external tools — only the Go toolchain.

Run it with:

go run build.go

The template is provided in assets/build.go. Copy it to the root of the project and adapt it to the project's needs.

Default Configuration

The default build.go ships with the following settings:

Setting Default Reason
CGO_ENABLED 0 Produces fully static binaries; required for cross-compilation without a C cross-compiler
linux/amd64 ✅ Standard server target
windows/amd64 ✅ Standard Windows desktop/server target
darwin/arm64 ✅ Apple Silicon Mac target

Customising the Build Script

All knobs are at the top of build.go:

Change the Binary Name

const (
    BinaryName   = "myapp" // ← rename to match the project
    C_GO_ENABLED = 0
)

Add or Remove Target Platforms

var platforms = []struct {
    OS   string
    Arch string
}{
    {"linux", "amd64"},
    {"windows", "amd64"},
    {"darwin", "arm64"},
    // {"darwin", "amd64"}, // Intel Mac
    // {"linux", "arm64"},  // ARM server / Raspberry Pi
}

Add Build Flags

Uncomment or add entries in buildOptions:

var buildOptions = []string{
    "-o", BinaryName,
    // "-v",                    // verbose output
    // "-trimpath",             // remove local file paths from binary
    // "-ldflags", "-s -w",     // strip debug symbols (smaller binary)
    // "-ldflags", "-X main.version=1.2.3", // embed version string
}

Enable CGO

Only enable CGO if the project requires C bindings (e.g., sqlite3, certain crypto libs). Cross-compilation with CGO requires a C cross-compiler per target platform.

const C_GO_ENABLED = 1 // requires C toolchain for each target

Common Mistakes

Mistake Fix
Placing build.go inside cmd/ Keep it at the repository root — it builds the whole project
Using CGO_ENABLED=1 without a cross-compiler Either disable CGO or install the correct C toolchain for each target
Hardcoding the output path without platform suffix When building for multiple platforms simultaneously, append _OS_ARCH to BinaryName to avoid overwriting
Forgetting -trimpath for release builds Release binaries should not embed developer machine paths

Related Skills

04 同仓库更多 Skills

paulohgodinho
paulohgodinho/skills

golang-structs-interfaces

Golang struct and interface design patterns — composition, embedding, type assertions, type switches, interface segregation, dependency injection via interfaces, struct field tags, and pointer vs value receivers. Use this skill when designing Go types, defining or implementing interfaces, embedding structs or interfaces, writing type assertions or type switches, adding struct field tags for JSON/YAML/DB serialization, or choosing between pointer and value receivers. Also use when the user asks about "accept interfaces, return structs", compile-time interface checks, or composing small interfaces into larger ones.

★ 0
paulohgodinho
paulohgodinho/skills

golang-project-layout

Provides a guide for setting up Golang project layouts and workspaces. Use this whenever starting a new Go project, organizing an existing codebase, setting up a monorepo with multiple packages, creating CLI tools with multiple main packages, or deciding on directory structure. Apply this for any Go project initialization or restructuring work.

★ 0
paulohgodinho
paulohgodinho/skills

golang-naming

Go (Golang) naming conventions — covers packages, constructors, structs, interfaces, constants, enums, errors, booleans, receivers, getters/setters, functional options, acronyms, test functions, and subtest names. Use this skill when writing new Go code, reviewing or refactoring, choosing between naming alternatives (New vs NewTypeName, isConnected vs connected, ErrNotFound vs NotFoundError, StatusReady vs StatusUnknown at iota 0), debating Go package names (utils/helpers anti-patterns), or asking about Go naming best practices. Also trigger when the user mentions MixedCaps vs snake_case, ALL_CAPS constants, Get-prefix on getters, or error string casing. Do NOT use for general Go implementation questions that don't involve naming decisions.

★ 0
paulohgodinho
paulohgodinho/skills

golang-lint

Provides linting best practices and golangci-lint configuration for Go projects. Covers running linters, configuring .golangci.yml, suppressing warnings with nolint directives, interpreting lint output, and managing linter settings. Use this skill whenever the user runs linters, configures golangci-lint, asks about lint warnings or suppressions, sets up code quality tooling, or asks which linters to enable for a Go project. Also use when the user mentions golangci-lint, go vet, staticcheck, revive, or any Go linting tool.

★ 0
paulohgodinho
paulohgodinho/skills

golang-design-patterns

Idiomatic Golang design patterns — functional options, constructors, error flow and cascading, resource management and lifecycle, graceful shutdown, resilience, architecture, dependency injection, data handling, and streaming. Apply when designing Go APIs, structuring applications, choosing between patterns, making design decisions, architectural choices, or production hardening.

★ 0
paulohgodinho
paulohgodinho/skills

golang-dependency-injection

Comprehensive guide for dependency injection (DI) in Golang. Covers why DI matters (testability, loose coupling, separation of concerns, lifecycle management), manual constructor injection, and DI library comparison (google/wire, uber-go/dig, uber-go/fx). Use this skill when designing service architecture, setting up dependency injection, refactoring tightly coupled code, managing singletons or service factories, or when the user asks about inversion of control, service containers, or wiring dependencies in Go.

★ 0