You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.4 KiB

# Go 1.21 环境安装脚本 (Windows PowerShell)
# 适用于 Windows 10/11
# 以管理员身份运行
param()
$ErrorActionPreference = "Stop"
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host " Go 1.21 环境安装脚本 (Windows) " -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 配置参数
$GoVersion = "1.21.6"
$DownloadUrl = "https://go.dev/dl/go$GoVersion.windows-amd64.msi"
$InstallerPath = "$env:TEMP\go$GoVersion.windows-amd64.msi"
# 检查当前是否已安装 Go
Write-Host "[1/5] 检查当前 Go 版本..." -ForegroundColor Yellow
$CurrentGo = Get-Command go -ErrorAction SilentlyContinue
if ($CurrentGo) {
$Version = & go version
Write-Host " 检测到已安装: $Version" -ForegroundColor Green
$response = Read-Host " 是否重新安装? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
Write-Host " 跳过安装" -ForegroundColor Gray
exit 0
}
} else {
Write-Host " 未检测到 Go" -ForegroundColor Gray
}
# 下载安装包
Write-Host "[2/5] 下载 Go $GoVersion ..." -ForegroundColor Yellow
Write-Host " 下载地址: $DownloadUrl" -ForegroundColor Gray
try {
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($DownloadUrl, $InstallerPath)
Write-Host " 下载完成: $InstallerPath" -ForegroundColor Green
} catch {
Write-Error "下载失败: $_"
exit 1
}
# 验证下载文件
if (-not (Test-Path $InstallerPath)) {
Write-Error "下载失败,文件不存在"
exit 1
}
$fileSize = (Get-Item $InstallerPath).Length / 1MB
Write-Host " 文件大小: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Gray
# 执行安装
Write-Host "[3/5] 开始安装 Go..." -ForegroundColor Yellow
Write-Host " 正在运行安装程序..." -ForegroundColor Gray
try {
$arguments = "/i `"$InstallerPath`" /passive /norestart"
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Error "安装失败,退出码: $($process.ExitCode)"
exit 1
}
Write-Host " 安装完成" -ForegroundColor Green
} catch {
Write-Error "安装过程出错: $_"
exit 1
}
# 设置环境变量
Write-Host "[4/5] 配置环境变量..." -ForegroundColor Yellow
$GoPath = "$env:USERPROFILE\go"
$GoBin = "$GoPath\bin"
# 检查 GOPATH
$CurrentGoPath = [Environment]::GetEnvironmentVariable("GOPATH", "User")
if (-not $CurrentGoPath) {
[Environment]::SetEnvironmentVariable("GOPATH", $GoPath, "User")
Write-Host " 设置 GOPATH: $GoPath" -ForegroundColor Green
}
# 更新 PATH - 添加 Goin
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($UserPath -notlike "*C:\Program Files\Go\bin*") {
$NewPath = $UserPath + ";C:\Program Files\Go\bin"
[Environment]::SetEnvironmentVariable("Path", $NewPath, "User")
Write-Host " 添加 Go 到 PATH" -ForegroundColor Green
}
# 添加 GOPATH\bin 到 PATH
if ($UserPath -notlike "*$GoBin*") {
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
$NewPath = $UserPath + ";" + $GoBin
[Environment]::SetEnvironmentVariable("Path", $NewPath, "User")
Write-Host " 添加 GOPATH/bin 到 PATH" -ForegroundColor Green
}
# 设置 GOPROXY
[Environment]::SetEnvironmentVariable("GOPROXY", "https://goproxy.cn,direct", "User")
Write-Host " 设置 GOPROXY: https://goproxy.cn,direct" -ForegroundColor Green
Write-Host " 环境变量配置完成" -ForegroundColor Green
# 验证安装
Write-Host "[5/5] 验证安装..." -ForegroundColor Yellow
# 刷新环境变量
$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [Environment]::GetEnvironmentVariable("Path", "User")
# 验证
try {
$GoExePath = "C:\Program Files\Go\bin\go.exe"
if (Test-Path $GoExePath) {
$GoVersionOutput = & $GoExePath version
Write-Host " Go 版本: $GoVersionOutput" -ForegroundColor Green
$GoEnvPath = & $GoExePath env GOPATH
Write-Host " GOPATH: $GoEnvPath" -ForegroundColor Green
$GoProxyVal = & $GoExePath env GOPROXY
Write-Host " GOPROXY: $GoProxyVal" -ForegroundColor Green
} else {
Write-Warning " 未找到 go.exe可能安装未完成"
}
} catch {
Write-Warning " 验证失败,请重新打开命令提示符后再次运行 'go version'"
}
# 清理
Write-Host "[清理] 删除安装包..." -ForegroundColor Gray
Remove-Item $InstallerPath -ErrorAction SilentlyContinue
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host " Go 安装完成! " -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "请重新打开 PowerShell 或命令提示符,然后运行:" -ForegroundColor Yellow
Write-Host " go version"
Write-Host ""
Write-Host "接下来可以启动行情数据服务:" -ForegroundColor Yellow
Write-Host " cd d:\fs_workspace\market-data-service"
Write-Host " go mod download"
Write-Host " go run ./cmd/server"
Write-Host ""
Write-Host "然后访问管理后台:" -ForegroundColor Yellow
Write-Host " http://localhost:8080/admin"
Write-Host ""
Read-Host "按 Enter 键退出"