Linux下一键安装Go语言最新版本环境的教程【最后更新:2018年06月13日】

曾经的旧版: https://www.skiy.net/201512023931.html

已将此项目独立出来了:
https://github.com/skiy/golang-install

如上所述,由于上一个版本的脚本更新GO最新版本时,每次都需要重新定义最新版本号.这样很不智能…
所以我就抽空花了点时间,学习了几个脚本和正则,终于重写了一个最新版本…

功能比之前更强大啦!

智能获取最新版本安装. 其它的功能跟原来的完全一致.

支持自定义版本。
支持 Linux / MacOS / FreeBSD 等系统。
支持 树莓派等ARM开发板.

本文地址: https://www.skiy.net/201805225164.html

注意: 此脚本需要在 root 下操作.

线上使用方式:

curl https://raw.githubusercontent.com/skiy/golang-install/master/install.sh | sh
或
wget -qO- https://raw.githubusercontent.com/skiy/golang-install/master/install.sh | sh

MY_DIY_GO_VERSION 为自定义的 golang 版本号,如:1.10.1
自定义版本方式:

curl -SL https://raw.githubusercontent.com/skiy/golang-install/master/install.sh | bash /dev/stdin MY_DIY_GO_VERSION
或
wget -qO- https://raw.githubusercontent.com/skiy/golang-install/master/install.sh | bash /dev/stdin MY_DIY_GO_VERSION

以下方式:

将最底部源代码保存为文件名为 go.install.sh 的文件.
执行,以下命令安装最新版本

sh go.install.sh

或者给此文件添加可执行权限,再进行操作.
只有添加可执行权限后,方可自定义版本.

# 添加可执行权限
chmod +x go.install.sh

# 安装最新版本
./go.install.sh

# 自定义版本
./go.install.sh 1.9.2

脚本如下:

#!/bin/bash
# Golang-Install
# Project Home Page:
# https://github.com/skiy/golang-install
#
# Author: Skiychan <dev@skiy.net>
# Link: https://www.skiy.net

set -e

RELEASES_URL="https://golang.google.cn/dl/"

# Get OS bit
initArch() {
    ARCH=$(uname -m)
    BIT=$ARCH
    case $ARCH in
        amd64) ARCH="amd64";;
        x86_64) ARCH="amd64";;
        i386) ARCH="386";;
        armv6l) ARCH="armv6l";; 
        armv7l) ARCH="armv6l";; 
        *) echo -e "\033[1;31mArchitecture ${ARCH} is not supported by this installation script\033[0m"; exit 1;;
    esac
    echo "ARCH = $ARCH"
}

# Get OS version
initOS() {
    OS=$(uname | tr '[:upper:]' '[:lower:]')
    case "$OS" in
        darwin) OS='darwin';;
        linux) OS='linux';;
        freebsd) OS='freebsd';;
#        mingw*) OS='windows';;
#        msys*) OS='windows';;
        *) echo -e "\033[1;31mOS ${OS} is not supported by this installation script\033[0m"; exit 1;;
    esac
    echo "OS = $OS"
}

# Compare Version
compareVersion() {
    OLD_VERSION="none"
    NEW_VERSION="$1"
    if test -x "$(command -v go)"; then
        OLD_VERSION="$(go version | awk '{print $3}')"
    fi
    if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
       echo -e "\033[1;31mYou have installed this version: $OLD_VERSION\033[0m"; exit 1;
    fi

printf "
Current version: \033[1;33m$OLD_VERSION\033[0m 
Target version: \033[1;33m$NEW_VERSION\033[0m
"
}

# Check if user is root
checkRoot() {
    ROOT=$(id -u)
    case "$ROOT" in
        0) ROOT='root';;
        *) echo -e "\033[1;31mError: You must be root to run this script\033[0m"; exit 1;;
    esac
}

# Download go file
downloadFile() {
    url="$1"
    destination="$2"

    echo -e "Fetching $url.. \n"
    if test -x "$(command -v curl)"; then
        code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
    elif test -x "$(command -v wget)"; then
        code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^  HTTP/{print $2}' | tail -1)
    else
        echo "Neither curl nor wget was available to perform http requests."
        exit 1
    fi

    if [ "$code" != 200 ]; then
        echo "Request failed with code $code"
        exit 1
    fi
}

# Set golang environment
setEnvironment() {
    profile="$1"
    if [ -z "`grep 'export\sGOROOT' $profile`" ];then
        echo "export GOROOT=/usr/local/go" >> $profile
    fi
    if [ -z "`grep 'export\sGOPATH' $profile`" ];then
        echo "export GOPATH=/data/go" >> $profile
    fi
    if [ -z "`grep 'export\sGOBIN' $profile`" ];then
        echo "export GOBIN=/data/go/bin" >> $profile
    fi   
    if [ -z "`grep '\$GOROOT/bin:\$GOBIN' $profile`" ];then
        echo "export PATH=\$GOROOT/bin:\$GOBIN:\$PATH" >> $profile
    fi  
}

# Printf version info
clear
printf "
###############################################################
###  Golang Install
###  Author Skiychan<dev@skiy.net>
###  Link https://www.skiy.net 
###############################################################
\n"

# identify platform based on uname output
checkRoot
initArch
initOS

# DIY version
if [ -n "$1" ] ;then
    RELEASE_TAG="go$1"
fi

# if RELEASE_TAG was not provided, assume latest
if [ -z "$RELEASE_TAG" ]; then
    RELEASE_TAG="$(curl -sL $RELEASES_URL | sed -n '/toggleVisible/p' | head -n 1 | cut -d '"' -f 4)"
fi
echo "Release Tag = $RELEASE_TAG"

# Compare version
compareVersion $RELEASE_TAG

printf "
###############################################################
###  System: %s 
###  Bit: %s 
###  Version: %s 
###############################################################
\n" $OS $BIT $RELEASE_TAG

# Download File
BINARY_URL="https://dl.google.com/go/$RELEASE_TAG.$OS-$ARCH.tar.gz"
DOWNLOAD_FILE="$(mktemp).tar.gz"
downloadFile "$BINARY_URL" "$DOWNLOAD_FILE"

# Tar file and move file
tar -C /usr/local/ -zxf $DOWNLOAD_FILE && \
rm  -rf $DOWNLOAD_FILE
 
# Create GOPATH folder
mkdir -p /data/go

# Set environmental for golang
PROFILE="/etc/profile"
setEnvironment "$PROFILE"
 
# Make environmental is enable
source $PROFILE
go env
go version
 
# Printf tip
printf "
###############################################################
# Install success, please execute again \e[1;33msource $PROFILE\e[0m
###############################################################
"
点赞