当前位置: 首页 > 编程笔记 >

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

储法
2023-03-14
本文向大家介绍linux中用shell快速安装配置Go语言的开发环境,包括了linux中用shell快速安装配置Go语言的开发环境的使用技巧和注意事项,需要的朋友参考一下

介绍

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的环境变量, 那么需要先删除这些配置,然后重新写入。

总结

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

 类似资料:
  • 主要内容:Windows安装,Linux安装本节我们来学习如何在各个平台上安装和配置R语言开发环境。 Windows安装 您可以从R-3.4.1 for Windows(32/64位)下载Windows的Windows安装程序版本,并将其保存在本地目录中。 打开下载页面如下所示 - 因为它是Windows安装程序(),下载的名称为“R-3.4.1-win.exe”。只需双击并运行接受默认设置的安装程序即可。 如果您的Windows是位版本,

  • 主要内容:安装Go语言开发包,配置环境变量,验证安装前面我们介绍了在 Windows 系统上来搭建Go语言开发包,本节将为大家讲解在 Linux 平台安装Go语言开发包,大家可以在Go语言官网找到对应的安装包( https://golang.google.cn/dl/),但是先不要急着下载。 提示:阅读本节需要对 Linux 系统及常用的命令有一定的了解,感兴趣的读者可以通过阅读《Linux入门教程》来了解更多 Linux 相关的知识。 注意:开发

  • Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Window 安装包下载地址为:https://golang.org/dl/。 如果打不开可以使用这个地址:https://golang.google.cn/dl/。 各个系统对应的包名: 操作系统 包名 Windows go1.4.windows-amd64.msi Linux go1.4.linux

  • 什么是 SDK 软件开发工具包(外语首字母缩写:SDK、外语全称:SoftwareDevelopmentKit)一般都是一些软件工程师为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件时的开发工具的集合 如果不安装SDK,你可以编写Go语言代码,但是你不能编译执行编写好的Go语言代码 如何安装? 1.下载 SDK 安装包。地址:https://golang.google.cn/dl/,由于

  • 记事本,开发效率极低 Vim,初学者入门门槛高 VSCode,不喜欢 Sublime Test,不喜欢 GoLand,喜欢,当收费 LiteIDE(开源免费, 跨平台运行,轻量级) 生男生女都一样, 最关键是你中意哪个就用哪个 Goland 安装 下载安装包:点我下载 Goland,提取码:lm7v 运行安装文件 疯狂下一步 激活程序:自行淘宝 JetBrains 激活,仅供学生党参考, 在职人员

  • 主要内容:.Net框架,集成开发环境(IDE)VB.Net,在Linux或Mac OS上编写VB.Net程序在本章中,我们将讨论和学习可用于创建VB.Net应用程序的工具。 我们已经提到,VB.Net是.Net框架的一部分,用于编写.Net应用程序。 因此,在讨论用于运行VB.Net程序的工具之前,先来了解VB.Net如何与.Net框架之间的相关联系。 .Net框架 .Net框架是一个革命性的平台,可以用于编写以下类型的应用程序: Windows应用程序 Web应用程序 网页服务(Web ser