|
| 1 | +# /go-build |
| 2 | + |
| 3 | +Build Go projects with Go-specific improvements. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +The `/go-build` command builds Go projects with intelligent defaults, cross-compilation support, and Go-specific optimizations. It provides build information, error suggestions, and supports modern Go build features. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```bash |
| 12 | +/go-build [options] [package] |
| 13 | +``` |
| 14 | + |
| 15 | +## Options |
| 16 | + |
| 17 | +| Option | Description | |
| 18 | +| --------------------- | -------------------------------------------------- | |
| 19 | +| `--output`, `-o FILE` | Output binary file name | |
| 20 | +| `--target OS/ARCH` | Build target (e.g., `linux/amd64`, `darwin/arm64`) | |
| 21 | +| `--race` | Enable race detector | |
| 22 | +| `--tags TAGS` | Build tags (comma-separated) | |
| 23 | +| `--build-mode MODE` | Build mode (`ex`, `pie`, `shared`, etc.) | |
| 24 | +| `--ldflags FLAGS` | Linker flags | |
| 25 | +| `--verbose`, `-v` | Verbose output | |
| 26 | +| `--clean` | Clean build artifacts before building | |
| 27 | +| `--cross-compile` | Cross-compile for multiple platforms | |
| 28 | +| `--help`, `-h` | Show help message | |
| 29 | + |
| 30 | +## Examples |
| 31 | + |
| 32 | +```bash |
| 33 | +# Build current project |
| 34 | +/go-build |
| 35 | + |
| 36 | +# Build with specific output name |
| 37 | +/go-build --output myapp |
| 38 | + |
| 39 | +# Build with race detector |
| 40 | +/go-build --race |
| 41 | + |
| 42 | +# Cross-compile for Linux |
| 43 | +/go-build --target linux/amd64 |
| 44 | + |
| 45 | +# Cross-compile for all platforms |
| 46 | +/go-build --cross-compile |
| 47 | + |
| 48 | +# Clean and build |
| 49 | +/go-build --clean |
| 50 | + |
| 51 | +# Build specific package |
| 52 | +/go-build ./cmd/myapp |
| 53 | + |
| 54 | +# Build with custom ldflags |
| 55 | +/go-build --ldflags "-X main.version=1.0.0 -X main.commit=$(git rev-parse HEAD)" |
| 56 | +``` |
| 57 | + |
| 58 | +## Go-Specific Features |
| 59 | + |
| 60 | +### Smart Output Management |
| 61 | + |
| 62 | +- Default output to `./bin/` directory |
| 63 | +- Automatic binary naming based on project type |
| 64 | +- Platform-specific extensions (`.exe` for Windows) |
| 65 | +- Output directory creation if not exists |
| 66 | + |
| 67 | +### Cross-Compilation Support |
| 68 | + |
| 69 | +- **Linux**: `amd64`, `arm64`, `arm` |
| 70 | +- **macOS**: `amd64`, `arm64` |
| 71 | +- **Windows**: `amd64`, `386` |
| 72 | +- Automatic `CGO_ENABLED=0` for static binaries |
| 73 | +- Platform-specific output naming |
| 74 | + |
| 75 | +### Build Information |
| 76 | + |
| 77 | +- Shows Go version and module info |
| 78 | +- Displays build constraints and flags |
| 79 | +- Provides output path information |
| 80 | +- Shows race detector status |
| 81 | +- Displays target platform |
| 82 | + |
| 83 | +### Error Suggestions |
| 84 | + |
| 85 | +- Provides fixes for common build errors |
| 86 | +- Suggests dependency management commands |
| 87 | +- Recommends tool installations |
| 88 | +- Offers workarounds for platform-specific issues |
| 89 | + |
| 90 | +### Build Optimization |
| 91 | + |
| 92 | +- Race detector integration |
| 93 | +- Build tag support |
| 94 | +- LDFLAGS for version embedding |
| 95 | +- Build mode selection |
| 96 | +- Cache management |
| 97 | + |
| 98 | +## Build Process |
| 99 | + |
| 100 | +### 1. Environment Detection |
| 101 | + |
| 102 | +- Checks Go version and tools |
| 103 | +- Detects Go module configuration |
| 104 | +- Identifies build constraints |
| 105 | +- Checks for race detector support |
| 106 | + |
| 107 | +### 2. Build Configuration |
| 108 | + |
| 109 | +- Applies configuration from `.opencode/go-config.json` |
| 110 | +- Sets build flags and ldflags |
| 111 | +- Configures output location |
| 112 | +- Sets up environment variables |
| 113 | + |
| 114 | +### 3. Build Execution |
| 115 | + |
| 116 | +- Executes `go build` with appropriate flags |
| 117 | +- Handles cross-compilation environment |
| 118 | +- Manages build cache |
| 119 | +- Captures build output |
| 120 | + |
| 121 | +### 4. Post-Build Actions |
| 122 | + |
| 123 | +- Shows build information |
| 124 | +- Verifies binary output |
| 125 | +- Provides next steps |
| 126 | +- Cleans up temporary files |
| 127 | + |
| 128 | +## Cross-Compilation |
| 129 | + |
| 130 | +### Supported Platforms |
| 131 | + |
| 132 | +```bash |
| 133 | +# Linux |
| 134 | +/go-build --target linux/amd64 |
| 135 | +/go-build --target linux/arm64 |
| 136 | +/go-build --target linux/arm |
| 137 | + |
| 138 | +# macOS |
| 139 | +/go-build --target darwin/amd64 |
| 140 | +/go-build --target darwin/arm64 |
| 141 | + |
| 142 | +# Windows |
| 143 | +/go-build --target windows/amd64 |
| 144 | +/go-build --target windows/386 |
| 145 | +``` |
| 146 | + |
| 147 | +### Batch Cross-Compilation |
| 148 | + |
| 149 | +```bash |
| 150 | +# Build for all supported platforms |
| 151 | +/go-build --cross-compile |
| 152 | + |
| 153 | +# Output files: |
| 154 | +# - myapp-linux-amd64 |
| 155 | +# - myapp-linux-arm64 |
| 156 | +# - myapp-darwin-amd64 |
| 157 | +# - myapp-darwin-arm64 |
| 158 | +# - myapp-windows-amd64.exe |
| 159 | +``` |
| 160 | + |
| 161 | +### Environment Variables |
| 162 | + |
| 163 | +Cross-compilation automatically sets: |
| 164 | + |
| 165 | +- `GOOS` - Target operating system |
| 166 | +- `GOARCH` - Target architecture |
| 167 | +- `CGO_ENABLED=0` - Disable CGO for static binaries |
| 168 | +- `GO111MODULE=on` - Enable Go modules |
| 169 | + |
| 170 | +## Build Flags |
| 171 | + |
| 172 | +### Common Flags |
| 173 | + |
| 174 | +- `-v` - Verbose output |
| 175 | +- `-race` - Race detector |
| 176 | +- `-tags` - Build constraints |
| 177 | +- `-ldflags` - Linker flags |
| 178 | +- `-buildmode` - Build mode |
| 179 | + |
| 180 | +### LDFLAGS Examples |
| 181 | + |
| 182 | +```bash |
| 183 | +# Version embedding |
| 184 | +/go-build --ldflags "-X main.version=1.0.0" |
| 185 | + |
| 186 | +# Commit hash embedding |
| 187 | +/go-build --ldflags "-X main.commit=$(git rev-parse HEAD)" |
| 188 | + |
| 189 | +# Build time embedding |
| 190 | +/go-build --ldflags "-X main.buildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 191 | + |
| 192 | +# Multiple flags |
| 193 | +/go-build --ldflags "\ |
| 194 | + -X main.version=1.0.0 \ |
| 195 | + -X main.commit=$(git rev-parse HEAD) \ |
| 196 | + -X main.buildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 197 | +``` |
| 198 | + |
| 199 | +## Build Modes |
| 200 | + |
| 201 | +### Available Modes |
| 202 | + |
| 203 | +- `exe` - Executable (default) |
| 204 | +- `pie` - Position Independent Executable |
| 205 | +- `shared` - Shared library |
| 206 | +- `c-shared` - C shared library |
| 207 | +- `c-archive` - C archive |
| 208 | + |
| 209 | +### Examples |
| 210 | + |
| 211 | +```bash |
| 212 | +# Position Independent Executable |
| 213 | +/go-build --build-mode pie |
| 214 | + |
| 215 | +# Shared library |
| 216 | +/go-build --build-mode shared |
| 217 | + |
| 218 | +# C shared library |
| 219 | +/go-build --build-mode c-shared |
| 220 | +``` |
| 221 | + |
| 222 | +## Error Handling |
| 223 | + |
| 224 | +### Common Errors and Fixes |
| 225 | + |
| 226 | +#### Module Not Found |
| 227 | + |
| 228 | +``` |
| 229 | +Error: cannot find module providing package github.com/example/pkg |
| 230 | +``` |
| 231 | + |
| 232 | +**Fix:** |
| 233 | + |
| 234 | +```bash |
| 235 | +/go-deps tidy |
| 236 | +# or |
| 237 | +go get github.com/example/pkg |
| 238 | +``` |
| 239 | + |
| 240 | +#### Undefined Symbol |
| 241 | + |
| 242 | +``` |
| 243 | +Error: undefined: SomeFunction |
| 244 | +``` |
| 245 | + |
| 246 | +**Fix:** |
| 247 | + |
| 248 | +- Check import statements |
| 249 | +- Verify function name spelling |
| 250 | +- Ensure package is imported correctly |
| 251 | + |
| 252 | +#### Imported and Not Used |
| 253 | + |
| 254 | +``` |
| 255 | +Error: imported and not used: "fmt" |
| 256 | +``` |
| 257 | + |
| 258 | +**Fix:** |
| 259 | + |
| 260 | +- Remove unused import |
| 261 | +- Or use blank identifier: `_ "fmt"` |
| 262 | + |
| 263 | +#### Missing go.sum Entry |
| 264 | + |
| 265 | +``` |
| 266 | +Error: missing go.sum entry for module |
| 267 | +``` |
| 268 | + |
| 269 | +**Fix:** |
| 270 | + |
| 271 | +```bash |
| 272 | +/go-deps tidy |
| 273 | +# or |
| 274 | +go mod download |
| 275 | +``` |
| 276 | + |
| 277 | +## Build Information Display |
| 278 | + |
| 279 | +After successful build, shows: |
| 280 | + |
| 281 | +``` |
| 282 | +📊 Build Information: |
| 283 | +======================================== |
| 284 | +Go: go version go1.21.0 darwin/amd64 |
| 285 | +Module: github.com/user/project |
| 286 | +Target: darwin/arm64 |
| 287 | +Race detector: enabled |
| 288 | +Output: /path/to/project/bin/myapp |
| 289 | +``` |
| 290 | + |
| 291 | +## Performance Tips |
| 292 | + |
| 293 | +### Build Cache |
| 294 | + |
| 295 | +- Uses Go build cache automatically |
| 296 | +- Cache location: `$GOCACHE` |
| 297 | +- Can be cleared with `--clean` flag |
| 298 | + |
| 299 | +### Parallel Building |
| 300 | + |
| 301 | +- Go builds modules in parallel |
| 302 | +- Use `-p` flag to control parallelism |
| 303 | +- Defaults to number of CPU cores |
| 304 | + |
| 305 | +### Incremental Builds |
| 306 | + |
| 307 | +- Subsequent builds are faster |
| 308 | +- Only changed packages are rebuilt |
| 309 | +- Cache invalidated on dependency changes |
| 310 | + |
| 311 | +## Integration |
| 312 | + |
| 313 | +### CI/CD Pipelines |
| 314 | + |
| 315 | +```yaml |
| 316 | +# GitHub Actions example |
| 317 | +jobs: |
| 318 | + build: |
| 319 | + runs-on: ubuntu-latest |
| 320 | + steps: |
| 321 | + - uses: actions/checkout@v3 |
| 322 | + - uses: actions/setup-go@v4 |
| 323 | + - run: /go-build --cross-compile |
| 324 | +``` |
| 325 | +
|
| 326 | +### Makefile Integration |
| 327 | +
|
| 328 | +```makefile |
| 329 | +.PHONY: build |
| 330 | +build: |
| 331 | + /go-build --output $(BINARY) |
| 332 | + |
| 333 | +.PHONY: build-all |
| 334 | +build-all: |
| 335 | + /go-build --cross-compile |
| 336 | + |
| 337 | +.PHONY: clean-build |
| 338 | +clean-build: |
| 339 | + /go-build --clean |
| 340 | +``` |
| 341 | +
|
| 342 | +## Exit Codes |
| 343 | +
|
| 344 | +| Code | Description | |
| 345 | +| ---- | --------------------------------- | |
| 346 | +| 0 | Build successful | |
| 347 | +| 1 | Build failed | |
| 348 | +| 2 | Configuration error | |
| 349 | +| 3 | Cross-compilation partial failure | |
| 350 | +
|
| 351 | +## Environment Variables |
| 352 | +
|
| 353 | +| Variable | Description | |
| 354 | +| ------------- | ----------------------- | |
| 355 | +| `GOOS` | Target operating system | |
| 356 | +| `GOARCH` | Target architecture | |
| 357 | +| `CGO_ENABLED` | CGO enable/disable | |
| 358 | +| `GO111MODULE` | Go modules mode | |
| 359 | +| `GOCACHE` | Build cache directory | |
| 360 | +| `GOPATH` | Go workspace path | |
| 361 | + |
| 362 | +## See Also |
| 363 | + |
| 364 | +- `/go-setup` - Configure Go project |
| 365 | +- `/go-test` - Run Go tests |
| 366 | +- `/go-lint` - Lint Go code |
| 367 | +- `/go-fmt` - Format Go code |
| 368 | +- `/go-deps` - Manage dependencies |
| 369 | +- [Go Build Command](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies) |
| 370 | +- [Cross-compilation Guide](https://go.dev/doc/install/source#environment) |
| 371 | +- [Build Constraints](https://pkg.go.dev/go/build#hdr-Build_Constraints) |
0 commit comments