脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - linux中用shell快速安装配置Go语言的开发环境

linux中用shell快速安装配置Go语言的开发环境

2020-05-03 12:55痕无落 Golang

相信每位开发者都知道选择一门开发语言,免不了需要安装配置开发环境,所以这篇文章给大家分享了linux下使用shell一键安装配置GO语言开发环境的方法,有需要的朋友们可以参考借鉴,下面来一起看看吧。

介绍

go1.5+版本提供编译好的安装包,我们只需要解压到相应的目录,并添加一些环境变量的配置即可。

Go语言的安装步骤

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

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

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

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

下面是安装脚本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/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的环境变量, 那么需要先删除这些配置,然后重新写入。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

延伸 · 阅读

精彩推荐
  • GolangGo语言range关键字循环时的坑

    Go语言range关键字循环时的坑

    今天小编就为大家分享一篇关于Go语言range关键字循环时的坑,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来...

    benben_20154202020-05-23
  • GolangGolang实现四种负载均衡的算法(随机,轮询等)

    Golang实现四种负载均衡的算法(随机,轮询等)

    本文介绍了示例介绍了Golang 负载均衡的四种实现,主要包括了随机,轮询,加权轮询负载,一致性hash,感兴趣的小伙伴们可以参考一下...

    Gundy_8442021-08-09
  • GolangGolang 语言极简类型转换库cast的使用详解

    Golang 语言极简类型转换库cast的使用详解

    本文我们通过 cast.ToString() 函数的使用,简单介绍了cast 的使用方法,除此之外,它还支持很多其他类型,在这没有多多介绍,对Golang 类型转换库 cast相关知...

    Golang语言开发栈6112021-12-02
  • Golang深入浅析Go中三个点(...)用法

    深入浅析Go中三个点(...)用法

    这篇文章主要介绍了深入浅析Go中三个点(...)用法,需要的朋友可以参考下...

    踏雪无痕SS6472021-11-17
  • GolangGo语言基础单元测试与性能测试示例详解

    Go语言基础单元测试与性能测试示例详解

    这篇文章主要为大家介绍了Go语言基础单元测试与性能测试示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助祝大家多多进步...

    枫少文7812021-12-05
  • Golanggo语言获取系统盘符的方法

    go语言获取系统盘符的方法

    这篇文章主要介绍了go语言获取系统盘符的方法,涉及Go语言调用winapi获取系统硬件信息的技巧,具有一定参考借鉴价值,需要的朋友可以参考下 ...

    无尽海3862020-04-24
  • GolangGo语言实现自动填写古诗词实例代码

    Go语言实现自动填写古诗词实例代码

    这篇文章主要给大家介绍了关于Go语言实现自动填写古诗词的相关资料,这是最近在项目中遇到的一个需求,文中通过示例代码介绍的非常详细,需要的朋...

    FengY5862020-05-14
  • GolangGO语言字符串处理Strings包的函数使用示例讲解

    GO语言字符串处理Strings包的函数使用示例讲解

    这篇文章主要为大家介绍了GO语言字符串处理Strings包的函数使用示例讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加...

    Jeff的技术栈6882022-04-14