-
-
Save denolfe/ab52e2fb548617486e72baf4fdce707e to your computer and use it in GitHub Desktop.
Install Golang using Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Installs golang on Windows. | |
# | |
# # Run script: | |
# .\install-go.ps1 -version 1.9.3 | |
# | |
# # Download and run script: | |
# $env:GOVERSION = '1.9.3' | |
# iex ((new-object net.webclient).DownloadString('SCRIPT_URL_HERE')) | |
Param( | |
[String]$version, | |
[switch]$h = $false, | |
[switch]$help = $false | |
) | |
$SCRIPT=$MyInvocation.MyCommand.Name | |
function print_usage() { | |
Write-Host @" | |
Download and install Golang on Windows. It sets the GOROOT environment | |
variable and adds GOROOT\bin to the PATH environment variable. | |
Usage: | |
$SCRIPT -version 1.9.3 | |
Options: | |
-h | -help | |
Print the help menu. | |
-version | |
Golang version to install. Required. | |
"@ | |
} | |
if ($help -or $h) { | |
print_usage | |
exit 0 | |
} | |
if ($args -ne "") { | |
Write-Error "Error: Unknown option $args" | |
print_usage | |
exit 1 | |
} | |
if ($version -eq "") { | |
$version = $env:GOVERSION | |
} | |
if ($version -eq "" ) { | |
Write-Error "Error: -version is required" | |
print_usage | |
exit 1 | |
} | |
$downloadDir = $env:TEMP | |
$url32 = 'https://storage.googleapis.com/golang/go' + $version + '.windows-386.zip' | |
$url64 = 'https://storage.googleapis.com/golang/go' + $version + '.windows-amd64.zip' | |
$goroot = "C:\go$version" | |
# Determine type of system | |
if ($ENV:PROCESSOR_ARCHITECTURE -eq "AMD64") { | |
$url = $url64 | |
} else { | |
$url = $url32 | |
} | |
if (Test-Path "$goroot\bin\go") { | |
Write-Host "Go is installed to $goroot" | |
exit | |
} | |
Write-Host "Downloading $url" | |
$zip = "$downloadDir\golang-$version.zip" | |
if (!(Test-Path "$zip")) { | |
$downloader = new-object System.Net.WebClient | |
$downloader.DownloadFile($url, $zip) | |
} | |
Write-Host "Extracting $zip to $goroot" | |
if (Test-Path "$downloadDir\go") { | |
Remove-Item -Force -Recurse -Path "$downloadDir\go" | |
} | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
[System.IO.Compression.ZipFile]::ExtractToDirectory("$zip", $downloadDir) | |
Move-Item "$downloadDir\go" $goroot | |
Write-Host "Setting GOROOT and PATH for Machine" | |
[System.Environment]::SetEnvironmentVariable("GOROOT", "$goroot", "Machine") | |
$p = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") | |
$p = "$goroot\bin;$p" | |
[System.Environment]::SetEnvironmentVariable("PATH", "$p", "Machine") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment