# 修复 Go 依赖脚本 param() $ErrorActionPreference = "Stop" Write-Host "==============================================" -ForegroundColor Cyan Write-Host " 修复 Go 依赖问题 " -ForegroundColor Cyan Write-Host "==============================================" -ForegroundColor Cyan Write-Host "" # 检查 Go 是否安装 Write-Host "[1/5] 检查 Go 环境..." -ForegroundColor Yellow $GoCmd = Get-Command go -ErrorAction SilentlyContinue if (-not $GoCmd) { Write-Error "未找到 Go 命令,请先安装 Go" exit 1 } $GoVersion = & go version Write-Host " Go 版本: $GoVersion" -ForegroundColor Green # 检查 GOPROXY Write-Host "[2/5] 检查 GOPROXY 设置..." -ForegroundColor Yellow $GoProxy = & go env GOPROXY Write-Host " 当前 GOPROXY: $GoProxy" -ForegroundColor Gray if ($GoProxy -ne "https://goproxy.cn,direct") { Write-Host " 设置国内镜像..." -ForegroundColor Yellow & go env -w GOPROXY="https://goproxy.cn,direct" Write-Host " GOPROXY 已设置为 https://goproxy.cn,direct" -ForegroundColor Green } # 检查 GOPATH Write-Host "[3/5] 检查 GOPATH..." -ForegroundColor Yellow $GoPath = & go env GOPATH Write-Host " GOPATH: $GoPath" -ForegroundColor Gray # 进入项目目录 Write-Host "[4/5] 进入项目目录..." -ForegroundColor Yellow $ProjectDir = "d:\fs_workspace\market-data-service" if (-not (Test-Path $ProjectDir)) { Write-Error "项目目录不存在: $ProjectDir" exit 1 } Set-Location $ProjectDir Write-Host " 当前目录: $(Get-Location)" -ForegroundColor Green # 清理缓存 Write-Host "[5/5] 修复依赖..." -ForegroundColor Yellow # 删除旧的模块缓存 Write-Host " 清理模块缓存..." -ForegroundColor Gray Remove-Item -Path "go.sum" -ErrorAction SilentlyContinue Remove-Item -Path "$GoPath\pkg\mod\cache" -Recurse -Force -ErrorAction SilentlyContinue # 设置环境变量 $env:GOPROXY = "https://goproxy.cn,direct" # 运行 go mod tidy Write-Host " 运行 go mod tidy..." -ForegroundColor Yellow try { & go mod tidy -v if ($LASTEXITCODE -eq 0) { Write-Host " go mod tidy 成功" -ForegroundColor Green } else { Write-Warning "go mod tidy 返回非零退出码: $LASTEXITCODE" } } catch { Write-Warning "go mod tidy 执行出错: $_" } # 下载依赖 Write-Host " 运行 go mod download..." -ForegroundColor Yellow try { & go mod download -x if ($LASTEXITCODE -eq 0) { Write-Host " go mod download 成功" -ForegroundColor Green } else { Write-Warning "go mod download 返回非零退出码: $LASTEXITCODE" } } catch { Write-Warning "go mod download 执行出错: $_" } # 验证 Write-Host " 验证依赖..." -ForegroundColor Yellow try { & go list -m all | Out-Null Write-Host " 依赖验证成功" -ForegroundColor Green } catch { Write-Warning "依赖验证失败: $_" } Write-Host "" Write-Host "==============================================" -ForegroundColor Cyan Write-Host " 依赖修复完成 " -ForegroundColor Cyan Write-Host "==============================================" -ForegroundColor Cyan Write-Host "" # 检查 go.sum 是否存在 if (Test-Path "go.sum") { Write-Host "✓ go.sum 文件已生成" -ForegroundColor Green } else { Write-Warning "✗ go.sum 文件未生成" } Write-Host "" Write-Host "接下来可以尝试编译项目:" -ForegroundColor Yellow Write-Host " go build ./cmd/server" -ForegroundColor Gray Write-Host "" Write-Host "或者直接运行:" -ForegroundColor Yellow Write-Host " go run ./cmd/server" -ForegroundColor Gray Write-Host "" Read-Host "按 Enter 键退出"