Skip to content

Batch files to setup a specific go version and batch files to switch to specific go version. #74407

Closed as not planned
@SkybuckFlying

Description

@SkybuckFlying

Proposal Details

Setup batch file (currently for manual GO build, because installer on windows is missing pkg.go in build folder ?):

filename: SetupGOVersion1.24.4v2.bat
contents:

@echo off
setlocal

echo.
echo =========================================================
echo Setting up Go environment variables for version 1.24.4.
echo =========================================================
echo.

echo IMPORTANT:
echo 1. Ensure you have Git and a C/C++ compiler (like MinGW-w64/TDM-GCC)
echo    installed and their 'bin' directories are in your system PATH.
echo    You can check this by typing: gcc --version
echo    (Expected output example: "gcc (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders, r8) 13.2.0")
echo 2. This script will:
echo    2.1. Create new directories and set permanent Go environment variables for Go 1.24.4.
echo         These variables will be set for the CURRENT USER.
echo    2.2. After you manually build Go from source (Step 3 in NEXT STEPS below),
echo         a new 'go.exe' for Go 1.24.4 will be placed in "G:\GOFOLDERS\version 1.24.4\GOBIN".
echo    2.3. AFTER THE BUILD, if you are keeping older Go installations, you MUST manually manage your user's PATH
echo         to ensure that "G:\GOFOLDERS\version 1.24.4\GOBIN" (or "G:\GOFOLDERS\version 1.24.4\GOROOT\bin")
echo         is listed BEFORE any paths to older 'go.exe' versions. This ensures your system uses the newly built Go 1.24.4.
echo.
echo The steps to manually clone and build Go 1.24.4 into the GOROOT directory created by this script are shown below.
echo.


:: Hardcoded paths for Go 1.24.4
SET "BASE_PATH=G:\GOFOLDERS\version 1.24.4"
SET "GOROOT_PATH=%BASE_PATH%\GOROOT"
SET "GOPATH_PATH=%BASE_PATH%\GOPATH"
SET "GOBIN_PATH=%BASE_PATH%\GOBIN"
SET "GOCACHE_PATH=%BASE_PATH%\GOCACHE"
SET "GOMODCACHE_PATH=%BASE_PATH%\GOMODCACHE"
SET "GOENV_PATH=%BASE_PATH%\GOENV\env"

:: Define Bootstrap Go version (Go 1.24 requires Go 1.22.6 or later for bootstrap)
SET "GO_BOOTSTRAP_VERSION=version 1.22.6"
SET "BOOTSTRAP_GOROOT_PATH=G:\GOFOLDERS\go-bootstrap\%GO_BOOTSTRAP_VERSION%\go"


echo Creating necessary directories...
mkdir "%GOPATH_PATH%" 2>nul
mkdir "%GOCACHE_PATH%" 2>nul
mkdir "%GOMODCACHE_PATH%" 2>nul
mkdir "%GOROOT_PATH%" 2>nul
mkdir "%GOBIN_PATH%" 2>nul
mkdir "%BASE_PATH%\GOENV" 2>nul
echo Directories created.

echo.
echo Configuring permanent Go environment variables using SETX (user-level)...
echo IMPORTANT: These changes will be applied to your *current user account* only,
echo and will take effect in NEW command prompt sessions.
echo.

:: Set GOROOT (user-level)
setx GOROOT "%GOROOT_PATH%"
:: Set GOPATH (user-level)
setx GOPATH "%GOPATH_PATH%"
:: Set GOBIN (user-level)
setx GOBIN "%GOBIN_PATH%"
:: Set GOCACHE (user-level)
setx GOCACHE "%GOCACHE_PATH%"
:: Set GOMODCACHE (user-level)
setx GOMODCACHE "%GOMODCACHE_PATH%"
:: Set GOENV (as a file path, not folder, user-level)
setx GOENV "%GOENV_PATH%"

echo ======================================================
echo  IMPORTANT: Please update the PATH environment variable
echo  in Windows manually to include:
echo  G:\GOFOLDERS\version 1.24.4\GOROOT\bin
echo  G:\GOFOLDERS\version 1.24.4\GOBIN
echo  You can do this via:
echo  Control Panel ^> System ^> Advanced System Settings
echo  Click "Environment Variables" ^> Edit "Path"
echo ======================================================

echo.
echo =========================================================
echo Go 1.24.4 Custom Environment Setup Complete!
echo =========================================================
echo.
echo NEXT STEPS (VERY IMPORTANT):
echo 0. PRE-BUILD FIXES (Required if previous build attempts failed with GDB or patch errors):
echo    0.1. Configure GDB for auto-loading:
echo         - Open the file: "C:\Users\skybu\.config\gdb\gdbinit" (create the folder and file if they don't exist).
echo         - Add the following line to that file:
echo           add-auto-load-safe-path G:\GOFOLDERS\version 1.24.4\GOROOT\src\runtime\runtime-gdb.py
echo         - This grants GDB permission to load Go's debugger helper script.
echo    0.2. Address 'patch.exe' issues (if encountered 'Assertion failed!' errors):
echo         - Ensure you have a reliable 'patch.exe' utility (e.g., from a Git for Windows installation).
echo         - Check your system's PATH environment variable (Control Panel > System > Advanced System Settings > Environment Variables > Path).
echo         - Make sure the directory containing a working 'patch.exe' (e.g., 'C:\Program Files\Git\usr\bin') is listed *before* any other directories that might contain an older or problematic 'patch.exe' (like 'G:\Tools\Strawberry\c\bin').
echo    0.3. Clean up previous failed build attempts:
echo         - Delete the Go 1.24.4 GOROOT directory: rmdir /s /q "%GOROOT_PATH%"
echo         - Delete the Go 1.24.4 GOCACHE directory: rmdir /s /q "%GOCACHE_PATH%"
echo         - Then, re-run *this* batch file to recreate the necessary directories and variables before proceeding.

echo 1. Open a *NEW* command prompt to ensure all environment variables are loaded.
echo 2. Manually clone Go 1.24.4 source into:
echo    "%GOROOT_PATH%"
echo    cd /d "%GOROOT_PATH%"
echo    git clone https://go.googlesource.com/go .
echo    git checkout go1.24.4
echo 3. Manually build Go from source:
echo    NOTE:
echo    - Download the Go 1.22.6.windows-amd64.zip from go.dev/dl
echo      and extract its content (the 'go' folder) into:
echo      "G:\GOFOLDERS\go-bootstrap\version 1.22.6\go"
echo      (e.g., this means extracting go1.22.6.windows-amd64.zip into G:\GOFOLDERS\go-bootstrap\version 1.22.6 so you get G:\GOFOLDERS\go-bootstrap\version 1.22.6\go)
echo    - To test the bootstrap compiler, consider temporarily removing any older Go versions
echo      from your system's PATH before running '.\all.bat'. This ensures '.\all.bat'
echo      will either use a minimal internally downloaded bootstrap or fail clearly.
echo    WARNING: GOROOT_BOOTSTRAP should ONLY be set temporarily for this build process.
echo             DO NOT set it as a permanent environment variable.
echo    set GOROOT_BOOTSTRAP=%BOOTSTRAP_GOROOT_PATH%
echo    cd /d "%GOROOT_PATH%\src"
echo    WARNING: You MUST run '.\all.bat' from an Administrator command prompt to prevent symbolic link creation problems during tests.
echo    .\all.bat
echo 4. Verify your Go installation:
echo    - In a NEW command prompt, type: go version
echo      (Expected output example: "go version go1.24.4 windows/amd64")
echo    - In a NEW command prompt, type: go env
echo      (Expected output example - full output from your system):
echo      C:\Users\skybu>go env
echo      set AR=ar
echo      set CC=gcc
echo      set CGO_CFLAGS=-O2 -g
echo      set CGO_CPPFLAGS=
echo      set CGO_CXXFLAGS=-O2 -g
echo      set CGO_ENABLED=1
echo      set CGO_FFLAGS=-O2 -g
echo      set CGO_LDFLAGS=-O2 -g
echo      set CXX=g++
echo      set GCCGO=gccgo
echo      set GO111MODULE=
echo      set GOAMD64=v1
echo      set GOARCH=amd64
echo      set GOAUTH=netrc
echo      set GOBIN=G:\GOFOLDERS\version 1.24.4\GOBIN
echo      set GOCACHE=G:\GOFOLDERS\version 1.24.4\GOCACHE
echo      set GOCACHEPROG=
echo      set GODEBUG=
echo      set GOENV=G:\GOFOLDERS\version 1.24.4\GOENV\env
echo      set GOEXE=.exe
echo      set GOEXPERIMENT=
echo      set GOFIPS140=off
echo      set GOFLAGS=
echo      set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=G:\Temp\go-build1519035250=/tmp/go-build -gno-record-gcc-switches
echo      set GOHOSTARCH=amd64
echo      set GOHOSTOS=windows
echo      set GOINSECURE=
echo      set GOMOD=NUL
echo      set GOMODCACHE=G:\GOFOLDERS\version 1.24.4\GOMODCACHE
echo      set GONOPROXY=
echo      set GONOSUMDB=
echo      set GOOS=windows
echo      set GOPATH=G:\GOFOLDERS\version 1.24.4\GOPATH
echo      set GOPRIVATE=
echo      set GOPROXY=https://proxy.golang.org,direct
echo      set GOROOT=G:\GOFOLDERS\version 1.24.4\GOROOT
echo      set GOSUMDB=sum.golang.org
echo      set GOTELEMETRY=off
echo      set GOTELEMETRYDIR=C:\Users\skybu\AppData\Roaming\go\telemetry
echo      set GOTMPDIR=
echo      set GOTOOLCHAIN=auto
echo      set GOTOOLDIR=G:\GOFOLDERS\version 1.24.4\GOROOT\pkg\tool\windows_amd64
echo      set GOVCS=
echo      set GOVERSION=go1.24.3
echo      set GOWORK=
echo      set PKG_CONFIG=pkg-config
echo.
pause

endlocal

filename: SwitchToGOVersion1.24.4v1.bat
contents:

@echo off

echo Switching to Go environment 1.24.4...

:: Set up Go environment variables
setx GOBIN "G:\GOFOLDERS\version 1.24.4\GOBIN"
setx GOCACHE "G:\GOFOLDERS\version 1.24.4\GOCACHE"
setx GOMODCACHE "G:\GOFOLDERS\version 1.24.4\GOMODCACHE"
:: Must be set to a file and not a folder
setx GOENV "G:\GOFOLDERS\version 1.24.4\GOENV\env"
setx GOPATH "G:\GOFOLDERS\version 1.24.4\GOPATH"
setx GOROOT "G:\GOFOLDERS\version 1.24.4\GOROOT"

:: Alert user to update PATH manually
echo ======================================================
echo  IMPORTANT: Please update the PATH environment variable
echo  in Windows manually to include:
echo  G:\GOFOLDERS\version 1.24.4\GOROOT\bin
echo  G:\GOFOLDERS\version 1.24.4\GOBIN
echo  You can do this via:
echo  Control Panel  System  Advanced System Settings
echo  Click "Environment Variables"  Edit "Path"
echo ======================================================

echo Go environment updated successfully! Restart your terminal.
pause

(Creating these batch files was tricky because of cmd.exe variable expansion limitations, other issues admin privileges, and other issues, this by-passes that by creating user environment variables, I hope these batch files will help somebody to work a bit easier with different go versions).

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions