linux中用shell快速安装配置Go语言的开发环境
作者:痕无落 时间:2024-02-12 22:38:11
介绍
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,go语言,安装
0
投稿
猜你喜欢
vue单页面在微信下只能分享落地页的解决方案
2024-05-09 10:52:19
.NET中书写XML的一种简单方法
2007-08-23 12:55:00
Python实现图形用户界面计算器
2022-09-02 06:07:22
如何通过Python收集MySQL MHA 部署及运行状态信息的功能
2024-01-13 22:14:44
浅谈Python 对象内存占用
2022-04-01 11:21:40
Pygame框架实现飞机大战
2023-09-15 02:14:24
python openCV自制绘画板
2022-05-17 23:45:51
Python 使用SFTP和FTP实现对服务器的文件下载功能
2023-10-29 09:39:40
thinkphp在模型中自动完成session赋值示例代码
2024-05-03 15:51:32
Python中几种导入模块的方式总结
2021-09-20 08:17:14
PyTorch和Keras计算模型参数的例子
2023-04-16 10:32:46
js的正则test,match,exec详细解析
2024-05-02 17:30:20
Django model反向关联名称的方法
2021-03-06 17:21:28
python如何将文件a.txt的内容复制到b.txt中
2021-04-24 15:22:33
python数据结构:数据类型
2022-05-05 01:54:19
回顾Javascript React基础
2023-07-13 00:57:00
如何优化SQL语句(全)
2024-01-27 06:12:59
Python进阶之自定义对象实现切片功能
2023-09-22 11:28:31
如何获取浏览器的更多信息?
2009-11-23 20:48:00
五个基于JS实现的炫酷登录页面
2024-04-17 09:43:25