| name | go-mod-helper |
| description | Go module system, dependency management, and project configuration assistance. |
Go Module Management Skill
Go module system, dependency management, and project configuration assistance.
Instructions
You are a Go module and dependency expert. When invoked:
-
Module Management:
- Initialize and configure Go modules
- Manage go.mod and go.sum files
- Handle module versioning and dependencies
- Work with module replacements and exclusions
- Configure module proxies and checksums
-
Dependency Management:
- Add, update, and remove dependencies
- Handle indirect dependencies
- Resolve version conflicts
- Use semantic import versioning
- Manage private modules
-
Project Setup:
- Initialize new Go projects
- Configure project structure
- Set up multi-module repositories
- Configure build tags and constraints
- Manage workspace mode
-
Troubleshooting:
- Fix module resolution errors
- Debug checksum mismatches
- Resolve import path issues
- Handle proxy and authentication problems
- Clean corrupted module cache
-
Best Practices: Provide guidance on Go module patterns, versioning, and dependency management
Go Module Basics
Module Initialization
go mod init github.com/username/project
go mod init example.com/myproject
mkdir -p cmd/server internal/api pkg/utils
touch cmd/server/main.go
cat > cmd/server/main.go << 'EOF'
package main
import (
"fmt"
"github.com/username/project/internal/api"
)
func main() {
fmt.Println("Hello, Go modules!")
api.Start()
}
EOF
go.mod File Structure
module github.com/username/project
go 1.21
require (
github.com/gin-gonic/gin v1.9.1
github.com/lib/pq v1.10.9
golang.org/x/sync v0.5.0
)
require (
github.com/bytedance/sonic v1.10.2
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d
github.com/gabriel-vasile/mimetype v1.4.3
)
replace (
github.com/old/package => ../local/package
github.com/original/repo => github.com/fork/repo v1.2.3
)
exclude (
github.com/bad/package v1.2.3
)
retract (
v1.5.0
[v1.6.0, v1.6.5]
)
go.sum File
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
Usage Examples
@go-mod-helper
@go-mod-helper --init-project
@go-mod-helper --update-deps
@go-mod-helper --tidy
@go-mod-helper --troubleshoot
@go-mod-helper --private-modules
Common Commands
Dependency Management
go get github.com/gin-gonic/gin
go get github.com/gin-gonic/gin@v1.9.1
go get github.com/gin-gonic/gin@latest
go get github.com/user/repo@commit-hash
go get github.com/user/repo@branch-name
go get -u github.com/gin-gonic/gin
go get -u ./...
go get -u=patch github.com/gin-gonic/gin
go mod tidy
go mod download
go mod verify
go mod graph
go mod why github.com/lib/pq
go list -m all
go list -m -versions github.com/gin-gonic/gin
Module Cache
go env GOMODCACHE
go clean -modcache
go mod download
go mod download github.com/gin-gonic/gin@v1.9.1
Project Setup Patterns
Standard Project Layout
myproject/
├── go.mod
├── go.sum
├── README.md
├── Makefile
├── .gitignore
├── cmd/
│ ├── server/
│ │ └── main.go
│ └── cli/
│ └── main.go
├── internal/
│ ├── api/
│ │ ├── handler.go
│ │ └── routes.go
│ ├── database/
│ │ └── db.go
│ └── config/
│ └── config.go
├── pkg/
│ ├── utils/
│ │ └── helper.go
│ └── models/
│ └── user.go
├── api/
│ └── openapi.yaml
├── docs/
│ └── design.md
├── scripts/
│ └── setup.sh
└── tests/
├── integration/
└── e2e/
Multiple Binaries
module github.com/username/project
go 1.21
package main
func main() {
}
package main
func main() {
}
go build -o bin/server ./cmd/server
go build -o bin/cli ./cmd/cli
go build ./cmd/...
go install ./cmd/server
go install ./cmd/cli
Library Project
module github.com/username/mylib
go 1.21
package mylib
package internal
Semantic Import Versioning
Major Versions (v2+)
module github.com/username/project/v2
go 1.21
require (
github.com/other/lib/v3 v3.1.0
)
import (
"github.com/username/project/v2/pkg/utils"
oldversion "github.com/username/project"
)
Version Strategy
v0.1.0
v0.2.0
v0.3.0
v1.0.0
v1.1.0
v1.1.1
v2.0.0
git tag v1.0.0
git push origin v1.0.0
Private Modules
Configure Private Repositories
go env -w GOPRIVATE="github.com/yourorg/*,gitlab.com/yourcompany/*"
export GOPRIVATE="github.com/yourorg/*"
git config --global url."git@github.com:".insteadOf "https://github.com/"
git config --global url."git@github.com:yourorg/".insteadOf "https://github.com/yourorg/"
go env -w GONOPROXY="github.com/yourorg/*"
go env -w GONOSUMDB="github.com/yourorg/*"
Authentication Methods
GitHub Personal Access Token
cat > ~/.netrc << EOF
machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_TOKEN
EOF
chmod 600 ~/.netrc
SSH Keys
ssh-keygen -t ed25519 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
git config --global url."git@github.com:".insteadOf "https://github.com/"
GitLab CI/CD
before_script:
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf "https://gitlab.com/"
- go env -w GOPRIVATE="gitlab.com/yourgroup/*"
Module Replacement
Local Development
module github.com/myorg/project
replace github.com/myorg/library => ../library
require github.com/myorg/library v1.2.3
Fork Replacement
replace github.com/original/repo => github.com/yourfork/repo v1.2.3
replace github.com/pkg/errors => github.com/pkg/errors v0.9.1
Temporary Fixes
go mod vendor
go build -mod=vendor
Workspace Mode (Go 1.18+)
Multi-Module Development
go work init
go work use ./project1
go work use ./project2
go work use ./shared-lib
cat go.work
go 1.21
use (
./project1
./project2
./shared-lib
)
replace github.com/myorg/shared => ./shared-lib
go work sync
go work edit
go work use -r .
echo "go.work" >> .gitignore
Vendoring
Enable Vendoring
go mod vendor
go build -mod=vendor
go env -w GOFLAGS=-mod=vendor
go mod vendor
go mod verify
Vendor Configuration
go build -mod=vendor
go build -mod=mod
go build -mod=readonly
Common Issues & Solutions
Issue: Checksum Mismatch
go clean -modcache
go mod download
go mod tidy
go mod verify
go get -u github.com/package/name
go mod tidy
go env GOSUMDB
Issue: Module Not Found
go list -m github.com/package/name
go get github.com/package/name
go mod download
go clean -modcache
go mod download
go list -m -json github.com/package/name
Issue: Version Conflicts
go mod graph | grep package-name
go mod why github.com/package/name
go get github.com/package/name@v1.2.3
exclude github.com/bad/package v1.2.3
Issue: Private Repository Access
go env -w GOPRIVATE="github.com/yourorg/*"
git config --global credential.helper store
git config --global url."git@github.com:".insteadOf "https://github.com/"
go env | grep GOPRIVATE
git config --get-regexp url
Issue: Slow Downloads
go env -w GOPROXY="https://proxy.golang.org,direct"
go env -w GOPROXY="https://goproxy.cn,direct"
go env -w GOPRIVATE="github.com/myorg/*"
go env -w GOPROXY="direct"
go env | grep PROXY
Issue: Corrupted Module Cache
go clean -modcache
go mod download
go mod verify
go env GOMODCACHE
Advanced Patterns
Build Constraints
package utils
package platform
Conditional Dependencies
require (
github.com/common/lib v1.0.0
)
require (
github.com/stretchr/testify v1.8.4
)
Module Documentation
package mypackage
go doc github.com/user/mypackage
go doc github.com/user/mypackage.FunctionName
godoc -http=:6060
Testing and CI/CD
Testing with Modules
go test ./...
go test -cover ./...
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
go test -race ./...
go test github.com/user/project/pkg/utils
CI/CD Configuration
GitHub Actions
name: Go
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: true
- name: Download dependencies
run: go mod download
- name: Verify dependencies
run: go mod verify
- name: Run go vet
run: go vet ./...
- name: Run tests
run: go test -v -race -coverprofile=coverage.out ./...
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
- name: Build
run: go build -v ./...
GitLab CI
image: golang:1.21
stages:
- test
- build
before_script:
- go mod download
test:
stage: test
script:
- go mod verify
- go vet ./...
- go test -v -race -coverprofile=coverage.out ./...
coverage: '/total:.*?(\d+\.\d+)%/'
build:
stage: build
script:
- go build -v ./...
artifacts:
paths:
- bin/
Build and Release
Build Configuration
go build -o bin/myapp ./cmd/myapp
VERSION=$(git describe --tags --always)
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
go build -ldflags "-X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME}" -o bin/myapp
GOOS=linux GOARCH=amd64 go build -o bin/myapp-linux-amd64
GOOS=darwin GOARCH=amd64 go build -o bin/myapp-darwin-amd64
GOOS=windows GOARCH=amd64 go build -o bin/myapp-windows-amd64.exe
go build -ldflags="-s -w" -o bin/myapp
CGO_ENABLED=0 go build -o bin/myapp
Makefile Example
.PHONY: build test clean install
VERSION ?= $(shell git describe --tags --always --dirty)
BUILD_TIME ?= $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS := -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)"
build:
go build $(LDFLAGS) -o bin/myapp ./cmd/myapp
test:
go test -v -race -coverprofile=coverage.out ./...
coverage:
go tool cover -html=coverage.out
lint:
golangci-lint run
tidy:
go mod tidy
go mod verify
clean:
rm -rf bin/
go clean -cache -modcache
install: build
cp bin/myapp $(GOPATH)/bin/
release:
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o bin/myapp-linux-amd64
GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o bin/myapp-darwin-amd64
GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o bin/myapp-windows-amd64.exe
Module Publishing
Prepare for Release
go test ./...
go mod tidy
go mod verify
golangci-lint run
Semantic Versioning
git tag v1.0.0
git push origin v1.0.0
module github.com/user/project/v2
git tag v2.0.0
git push origin v2.0.0
git tag v1.0.0-beta.1
git tag v1.0.0-rc.1
Publishing Checklist
Best Practices Summary
Module Management
- Use semantic versioning (v1.2.3)
- Tag releases properly
- Keep go.mod and go.sum in version control
- Run
go mod tidy regularly
- Use
go mod verify before releases
- Document breaking changes
Dependency Management
- Minimize dependencies
- Use
require for direct dependencies
- Let Go manage indirect dependencies
- Update dependencies regularly
- Test after updates
- Use
go.sum for verification
Project Structure
- Follow standard project layout
- Use internal/ for private packages
- Use pkg/ for public libraries
- Keep cmd/ for binaries
- Organize by feature, not by type
Versioning
- Start with v0.x.x for development
- Use v1.0.0 for first stable release
- v2+ requires /v2 in module path
- Use pre-release tags (beta, rc)
- Never delete published versions
Security
- Run
go mod verify regularly
- Use checksums (go.sum)
- Audit dependencies
- Keep dependencies updated
- Use GOSUMDB for verification
Performance
- Use module cache
- Enable module proxy
- Vendor for Docker builds
- Use
go mod download in CI
Quick Reference Commands
go mod init <module-path>
go get <package>
go get -u <package>
go mod tidy
go mod download
go mod verify
go list -m all
go mod graph
go mod why <package>
go list -m -versions <package>
go clean -modcache
go mod vendor
go get -u ./...
go build ./...
go test ./...
go install ./...
Notes
- Always commit go.mod and go.sum files
- Use semantic versioning for releases
- Tag releases with git tags
- Use module proxy for faster downloads
- Configure GOPRIVATE for private modules
- Use workspaces for multi-module development
- Vendor dependencies for Docker builds
- Keep module path stable (avoid renames)
- Document breaking changes clearly
- Use
go mod tidy before committing