Agent Skills
› nixopus/nixopus
› go-deploy
go-deploy
GitHub提供Go项目的构建与部署指南,涵盖版本检测、静态二进制编译、CGO配置、工作区处理及Dockerfile最佳实践。
Trigger Scenarios
部署Go应用
检测到go.mod文件
Install
npx skills add nixopus/nixopus --skill go-deploy -g -y
SKILL.md
Frontmatter
{
"name": "go-deploy",
"metadata": {
"version": "1.0"
},
"description": "Build and deploy Go applications — version detection, static binaries, CGO, workspaces, and Dockerfile patterns. Use when deploying a Go project, or when go.mod is detected."
}
Go Deployment
Detection
Project is Go if any of these exist:
go.modat the build context rootgo.workat the root (Go workspaces)main.goat the root
Versions
Go version priority:
go.mod→godirective (e.g.go 1.21).go-versionfile,mise.toml, or.tool-versions- Defaults to 1.23
Build
Build Command
- Single module:
go build -ldflags="-w -s" -o /app/out . - CGO disabled (static):
CGO_ENABLED=0 go build -ldflags="-w -s" -o /app/out . - To build a specific binary from
cmd/, target it directly:go build -ldflags="-w -s" -o /app/<name> ./cmd/<name> - For workspaces, specify the module path:
go build -ldflags="-w -s" -o /app/out ./<module>
Main Package Resolution
- Root directory if it contains
.gofiles - First subdirectory in
cmd/(e.g../cmd/server) - For workspaces (
go.work): first module with amainpackage
Output
- Binary:
/app/outor/app/<name> - Static binary when
CGO_ENABLED=0
Go Workspaces
For multi-module projects with go.work:
- Discovers and copies all module dependencies
- Builds first module with
main.goby default - Specify the target module path to build a different one
CGO Support
Default: CGO disabled (CGO_ENABLED=0) for static binaries. If CGO needed:
- Set
CGO_ENABLED=1 - Build stage needs:
gcc,g++,libc6-dev - Runtime needs
libc6for dynamic linking - Use
debian:bookworm-sliminstead ofalpinefor runtime
Port Detection
PORTin.env/.env.example- Source code patterns:
:8080,ListenAndServe,Run(inmain.go - Default: 8080
Framework / Library Detection
| Import / package | Category |
|---|---|
net/http |
Standard library |
github.com/gin-gonic/gin |
Gin |
github.com/labstack/echo |
Echo |
github.com/go-chi/chi |
Chi |
github.com/valyala/fasthttp |
FastHTTP |
github.com/gofiber/fiber |
Fiber |
Install Stage Optimization
Copy in order:
go.mod,go.sum(andgo.workif workspace)- Workspace: copy all module directories referenced in
go.work *.go(or full source for multi-package)
Copy go.mod + go.sum first for layer caching.
Caching
Use BuildKit cache mount:
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -ldflags="-w -s" -o /app/out .
Base Images
| Stage | Image |
|---|---|
| Build | golang:1.23-alpine or golang:1.23-bookworm |
| Runtime (static) | gcr.io/distroless/static or alpine:latest |
| Runtime (CGO) | debian:bookworm-slim |
Dockerfile Patterns
Static Binary (Alpine runtime)
FROM golang:1.23-alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /app/out .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=build /app/out .
EXPOSE 8080
CMD ["./out"]
Distroless Runtime
FROM golang:1.23-bookworm AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /app/out .
FROM gcr.io/distroless/static
WORKDIR /app
COPY --from=build /app/out .
EXPOSE 8080
CMD ["./out"]
cmd layout
FROM golang:1.23-alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /app/server ./cmd/server
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=build /app/server .
EXPOSE 8080
CMD ["./server"]
Go workspace
FROM golang:1.23-alpine AS build
WORKDIR /app
COPY go.work go.work.sum ./
COPY api/go.mod api/go.sum ./api/
COPY shared/go.mod shared/go.sum ./shared/
RUN go mod download ./api/...
COPY api/ ./api/
COPY shared/ ./shared/
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /app/out ./api
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=build /app/out .
EXPOSE 8080
CMD ["./out"]
Gotchas
CGO_ENABLED=0is required for truly static binaries — without it, the binary may dynamically link glibc and fail on Alpine/distrolessgo.summust be committed to the repo — missing it causesgo mod downloadto fail in Docker- Multi-binary repos with
cmd/layout require specifying the target:go build ./cmd/server, not justgo build . - Alpine runtime images need
ca-certificatesfor outbound HTTPS — distroless/static includes them by default - Go modules cache at
/go/pkg/mod— use BuildKit cache mounts to avoid re-downloading on every build
Version History
- cf05d97 Current 2026-08-20 14:52


