linux中用shell快速安装配置Go语言的开发环境
来源:脚本之家
时间:2023-01-07 11:48:14 223浏览 收藏
本篇文章向大家介绍《linux中用shell快速安装配置Go语言的开发环境》,主要包括安装、linuxgo语言,具有一定的参考价值,需要的朋友可以参考一下。
介绍
go1.5+版本提供编译好的安装包,我们只需要解压到相应的目录,并添加一些环境变量的配置即可。
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的修改生效
这里有一些主意的点是,有可能系统配置过golang的环境变量, 那么需要先删除这些配置,然后重新写入。
总结
理论要掌握,实操不能落!以上关于《linux中用shell快速安装配置Go语言的开发环境》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
-
318 收藏
-
358 收藏
-
186 收藏
-
269 收藏
-
275 收藏
-
353 收藏
-
410 收藏
-
366 收藏
-
183 收藏
-
419 收藏
-
266 收藏
-
352 收藏
-
491 收藏
-
277 收藏
-
390 收藏
-
170 收藏
-
116 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习