linux安装go环境教程,Linux下⼀键安装Go语⾔最新版本环境的
教程
有点 Shell 基础,⽽且经常安装 Golang 的环境,为了⽅便⾃⼰,就写了个⼀键安装 golang 的环境教程,之前写过⼀次,但是不够智能,现在添加了⼏个功能。
1、可以⾃定义 Golang 版本号;
2、⾃动判断系统的相关信息(如32位or64位系统)及下载对应的地址
MY_DIY_GO_VERSION 为⾃定义的 golang 版本号,如:1.6.2
###第⼀种⽅式: 直接通过我写好的脚本⼀键安装(⼀键安装)。
截⽌发⽂时,Go 版本号为:1.7rc3
### Ubuntu/CentOS:
> 脚本默认版本号
> ⾃定义版本号
注意:
建议在上⾯的 URL 后添加⼀些参数,防⽌脚本的缓存过旧。
###第⼆种⽅式:
将下⾯的代码复制到Linux服务器下任意⼀个⽬录,并将⽂件名改成 "xxx.sh",再执⾏ sh xxx.sh MY_DIY_GO_VERSION
#!/bin/bash
# Author: Skiychan
#
#
# Project home page:
# Set Golang Version
go_version="1.7rc3"
# DIY version
if [ -n "$1" ] ;then
go_version=$1
fi
# Printf Version Info
clear
printf "
>>>>>>>>#
# Author Skiychan #
>>>>>>>>#
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
echo=echo
for cmd in echo /bin/echo; do
$cmd >/dev/null 2>&1 || continue
if ! $cmd -e "" | grep -qE '^-e'; then
echo=$cmd
break
fi
done
# Set Color
CSI=$($echo -e "\033[")
go语言安装教程CEND="${CSI}0m"
CDGREEN="${CSI}32m"
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
CYELLOW="${CSI}1;33m"
CBLUE="${CSI}1;34m"
CMAGENTA="${CSI}1;35m"
CCYAN="${CSI}1;36m"
CSUCCESS="$CDGREEN"
CFAILURE="$CRED"
CQUESTION="$CMAGENTA"
CWARNING="$CYELLOW"
CMSG="$CCYAN"
#Check Linux Version
if [ -f /etc/redhat-release -o -n "`grep 'Aliyun Linux release' /etc/issue`" ];then
OS=CentOS
[ -n "`grep ' 7\.' /etc/redhat-release`" ] && CentOS_RHEL_version=7
[ -n "`grep ' 6\.' /etc/redhat-release`" -o -n "`grep 'Aliyun Linux release6 15' /etc/issue`" ] && CentOS_RHEL_version=6 [ -n "`grep ' 5\.' /etc/redhat-release`" -o -n "`grep 'Aliyun Linux release5' /etc/issue`" ] && CentOS_RHEL_version=5
elif [ -n "`grep bian /etc/issue`" ];then
OS=Debian
Debian_version=`lsb_release -sr | awk -F. '{print $1}'`
elif [ -n "`grep Ubuntu /etc/issue`" ];then
OS=Ubuntu
Ubuntu_version=`lsb_release -sr | awk -F. '{print $1}'`
else
echo "${CFAILURE}Does not support this OS, Please contact the author! ${CEND}"
kill -9 $$
fi
# Check Linux Bit
if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LONG_BIT) = '64' ] ; then
sys_bit="amd64"
else
sys_bit="386"
fi
printf "
>>>>>>>>#
# System: %s #
# Bit: %s #
# Golang %s Install #
>>>>>>>>#
\n" $OS $sys_bit $go_version
# Install Curl Lib for Linux
if [ $OS = 'CentOS' ];then
yum update -y
yum install -y curl
else
apt-get update -y
apt-get install -y curl
fi
# Download Golang Package, Then Unzip to /usr/local
printf "
download url: %s
\n" $GO_DOWNLOAD
rm -rf
curl --retry 10 --retry-delay 60 --retry-max-time 60 -C - -SL -o $GO_DOWNLOAD && \
tar -C /usr/local/ -zxf && \
rm -rf
# Create GOPATH
mkdir -p /data/go
# Set ENV for Golang
cat <> /etc/profile
export GOROOT=/usr/local/go
export GOPATH=/data/go
export PATH=\$GOROOT/bin:\$PATH
EOF
# Make Env Is Enable
source /etc/profile
go env
go version
# Printf Tip
printf "
>>>>>>>>># # 安装成功,请再次执⾏ source /etc/profile #
>>>>>>>>># "
###注意
安装成功,请再次执⾏ source /etc/profile

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论