网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在linux中使用shell安装Go语言开发环境-创新互联

这篇文章将为大家详细讲解有关如何在linux中使用shell安装Go语言开发环境,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有灌南免费网站建设让你可以放心的选择与我们合作。

Go语言的安装步骤

     下载安装包go1.7.linux-amd64.tar.gz

     解压文件到指定目录: tar -zxf go1.7.linux-amd64.tar.gz

     设置环境变量:GOROOT, GOPATH, PATH

既然我们可以列出这些步骤,那么便可以将整个过程自动化。

下面是安装脚本

#!/bin/bash
#Upgrade go version to 1.7
#wget https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz go1.7.tar.gz

function info() {
 echo -e "\033[1;34m$1 \033[0m"
}

function warn() {
 echo -e "\033[0;33m$1 \033[0m"
}

function error() {
 echo -e "\033[0;31m$1 \033[0m"
}

function usage() {
 info "Upgrade or install golang..."
 info "USAGE:"
 info " ./upgrade.sh tar_file gopath"
 info " tar_file specify where is the tar file of go binary file"
 info " gopath specify where is the go workspace, include src, bin, pkg folder"
}

function createGoPath() {
 if [ ! -d $1 ];
 then
 mkdir -p $1
 fi
 if [ ! -d "$1/src" ];
 then
 mkdir "$1/src"
 fi
 if [ ! -d "$1/bin" ];
 then
 mkdir "$1/bin"
 fi
 if [ ! -d "$1/pkg" ];
 then
 mkdir "$1/pkg"
 fi
}

if [ -z $1 ];
then
 usage
 exit 1
fi

file=$1
if [ ! -f $file ];
then
 error "${file} not exist..."
 exit 1
fi

unzipPath="`pwd`/tmp_unzip_path/"
info $unzipPath

if [ ! -d $unzipPath ];
then
 info "not exist"
 mkdir $unzipPath
fi

tar -zxf $file -C $unzipPath

goroot=$GOROOT
if [ ! -n $GOROOT ];
then
 warn "Use default go root /usr/local/go"
 goroot="/usr/local/go"
fi

gopath=$2
info "Create go workspace, include src,bin,pkg folder..."
if [ -z $2 ];
 then
 user=`whoami`
 gopath="/home/$user/workspace/golang"
 warn "Use $gopath as golang workspace..."
 if [ ! -d $gopath ];
 then
 mkdir -p $gopath
 fi
fi

createGoPath $gopath

info "Copy go unzip files to $goroot"
sudo cp -r "$unzipPath/go" $goroot
rm -rf $unzipPath

#etcProfile="/home/user/Desktop/etc"

etcProfile="/etc/profile"
exportGoroot="export GOROOT=$goroot"
if [ ! -z $GOROOT ];
then
 cat $etcProfile | sed 's/^export.GOROOT.*//' | sudo tee $etcProfile > /dev/null
fi
echo $exportGoroot | sudo tee -a $etcProfile

exportGopath="export GOROOT=$gopath"
if [ ! -z $GOPATH ];
then
 cat $etcProfile | sed 's/^export.GOPATH.*//' | sudo tee $etcProfile > /dev/null
fi
echo "export GOPATH=$gopath" | sudo tee -a $etcProfile

echo 'export PATH=$GOROOT/bin:$GOPATH/bin:$PATH' | sudo tee -a $etcProfile

# ## Replace multiple empty lines with one empty line
cat $etcProfile -s | sudo tee $etcProfile > /dev/null

info "To make configuration take effect, will reboot, pls enter[y/n]"
read -p "[y/n]" isReboot
if [ $isReboot = "y" ];
then
 sudo reboot
fi

讲一讲脚本做的事情吧

     1、脚本要求输入编译好的安装包,这里本来是可以做成直接下载的, 但是考虑到大多数人是无法连接到golang的官网的,因此放弃了这一步。

     2、解压文件到指定的目录, 默认为/usr/local/go , 也可以通过运行时指定

     3、在GOPATH下面创建3个文件夹: src, bin, pkg, GOPATH可以运行时指定,默认为/home/{user}/workspace/golang

     4、设置环境变量: $GOPATH, $GOROOT

     5、重启服务,使对/etc/profile的修改生效

关于如何在linux中使用shell安装Go语言开发环境就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:如何在linux中使用shell安装Go语言开发环境-创新互联
分享URL:http://bjjierui.cn/article/dgceid.html

其他资讯