#!/bin/bash

# Tentukan versi Go terbaru
GO_VERSION="1.22.3"
GO_TAR="go${GO_VERSION}.linux-amd64.tar.gz"
GO_URL="https://go.dev/dl/${GO_TAR}"

# Unduh dan ekstrak Go
wget ${GO_URL}
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf ${GO_TAR}

# Bersihkan file arsip
rm ${GO_TAR}

# Tambahkan Go ke PATH (jika belum ada)
PROFILE_FILE="$HOME/.bashrc"
if ! grep -q "/usr/local/go/bin" "$PROFILE_FILE"; then
  echo 'export PATH=$PATH:/usr/local/go/bin' >> "$PROFILE_FILE"
fi

# Terapkan perubahan PATH
source "$PROFILE_FILE"

# Buat workspace Go
mkdir -p ~/go/{bin,pkg,src}

# Tambahkan pengaturan workspace ke environment
if ! grep -q "export GOPATH=" "$PROFILE_FILE"; then
  echo 'export GOPATH=$HOME/go' >> "$PROFILE_FILE"
  echo 'export PATH=$PATH:$GOPATH/bin' >> "$PROFILE_FILE"
fi

# Terapkan perubahan GOPATH
source "$PROFILE_FILE"
source ~/.bashrc

# Verifikasi instalasi
echo "✅ Go version:"
go version

echo "📁 Workspace dibuat di: ~/go"
echo "✅ Instalasi dan konfigurasi selesai!"

📌 Cara Menjalankan Skrip

  1. Salin skrip di atas ke file bernama install-go.sh.
  2. Jalankan perintah berikut di terminal:
chmod +x install-go.sh
./install-go.sh
source ~/.bashrc