You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/go-installer.md
+32-19Lines changed: 32 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,20 +6,39 @@ author: "Kapil Agrawal"
6
6
comments: false
7
7
---
8
8
9
-
I wrote a super simple shell script to install Go on a linux system to get a consistent Go environment for all users. I often use this script when I need Go installed on a remote development VM specialy when working on projects that require x86-64 architecture.
9
+
I asked our LLM overlords to generate a simple shell script to install Go on a linux system so as to get a consistent development environment regardless of the underlying OS architecture (amd64 vs. arm) I am working on. By default it installs the latest release. To install a specific go version pass it as a cli argument when running the script.
10
10
11
11
```sh
12
12
#!/bin/bash
13
13
14
-
set -e# Exit on error
15
-
set -o pipefail# Catch errors in pipelines
14
+
set -e
15
+
set -o pipefail
16
16
17
-
VERSION=${1:-"1.24.1"}
18
-
ARCH="linux-amd64"
17
+
# Detect architecture
18
+
ARCH_RAW=$(uname -m)
19
+
case"$ARCH_RAW"in
20
+
x86_64) GO_ARCH="amd64" ;;
21
+
aarch64 | arm64) GO_ARCH="arm64" ;;
22
+
*)
23
+
echo"❌ Unsupported architecture: $ARCH_RAW">&2
24
+
exit 1
25
+
;;
26
+
esac
27
+
28
+
# Get version (default: latest)
29
+
if [ -n"$1" ];then
30
+
VERSION="$1"
31
+
else
32
+
echo"📦 Fetching latest Go version..."
33
+
VERSION=$(curl -s https://go.dev/VERSION?m=text | head -n 1 | sed 's/go//')
34
+
fi
35
+
36
+
ARCH="linux-${GO_ARCH}"
19
37
GO_TARBALL="go${VERSION}.${ARCH}.tar.gz"
20
38
GO_URL="https://go.dev/dl/${GO_TARBALL}"
21
39
INSTALL_DIR="/usr/local/go"
22
40
41
+
# Root check
23
42
if [ "$(id -u)"-ne 0 ];then
24
43
echo"This script must be run as root or with sudo.">&2
25
44
exit 1
@@ -34,32 +53,26 @@ fi
34
53
# Remove old Go binaries
35
54
ifcommand -v go &>/dev/null;then
36
55
echo"Removing existing Go binaries..."
37
-
rm -f $(which go)$(which gofmt)
56
+
rm -f "$(command -v go)""$(command -v gofmt)"
38
57
fi
39
58
40
59
# Download and install Go
41
60
if wget --quiet --spider "$GO_URL";then
42
-
echo"Downloading Go $VERSION..."
61
+
echo"⬇️ Downloading Go $VERSION for $GO_ARCH..."
43
62
wget -q "$GO_URL" -O "$GO_TARBALL"
44
-
echo"Extracting Go..."
63
+
echo"📦 Extracting Go..."
45
64
tar -C /usr/local -xvf "$GO_TARBALL"
46
65
rm "$GO_TARBALL"
47
66
else
48
-
echo"Error: Go version $VERSION not found at $GO_URL">&2
67
+
echo"❌ Error: Go version $VERSION not found at $GO_URL">&2
0 commit comments