linux中用shell快速安裝配置Go語(yǔ)言的開發(fā)環(huán)境
介紹
go1.5+版本提供編譯好的安裝包,我們只需要解壓到相應(yīng)的目錄,并添加一些環(huán)境變量的配置即可。
Go語(yǔ)言的安裝步驟
下載安裝包go1.7.linux-amd64.tar.gz
解壓文件到指定目錄: tar -zxf go1.7.linux-amd64.tar.gz
設(shè)置環(huán)境變量:GOROOT, GOPATH, PATH
既然我們可以列出這些步驟,那么便可以將整個(gè)過(guò)程自動(dòng)化。
下面是安裝腳本
#!/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、腳本要求輸入編譯好的安裝包,這里本來(lái)是可以做成直接下載的, 但是考慮到大多數(shù)人是無(wú)法連接到golang的官網(wǎng)的,因此放棄了這一步。
2、解壓文件到指定的目錄, 默認(rèn)為/usr/local/go , 也可以通過(guò)運(yùn)行時(shí)指定
3、在GOPATH下面創(chuàng)建3個(gè)文件夾: src, bin, pkg, GOPATH可以運(yùn)行時(shí)指定,默認(rèn)為/home/{user}/workspace/golang
4、設(shè)置環(huán)境變量: $GOPATH, $GOROOT
5、重啟服務(wù),使對(duì)/etc/profile的修改生效
這里有一些主意的點(diǎn)是,有可能系統(tǒng)配置過(guò)golang的環(huán)境變量, 那么需要先刪除這些配置,然后重新寫入。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- VsCode搭建Go語(yǔ)言開發(fā)環(huán)境的配置教程
- GO語(yǔ)言運(yùn)行環(huán)境下載、安裝、配置圖文教程
- win7下配置GO語(yǔ)言環(huán)境 + eclipse配置GO開發(fā)
- Ubuntu下安裝Go語(yǔ)言開發(fā)環(huán)境及編輯器的相關(guān)配置
- VS Code配置Go語(yǔ)言開發(fā)環(huán)境的詳細(xì)教程
- vim配置go語(yǔ)言語(yǔ)法高亮問(wèn)題的解決方法
- 使用Go語(yǔ)言實(shí)現(xiàn)配置文件熱加載功能
- Mac下Vs code配置Go語(yǔ)言環(huán)境的詳細(xì)過(guò)程
- 使用VSCODE配置GO語(yǔ)言開發(fā)環(huán)境的完整步驟
- 云端golang開發(fā),無(wú)需本地配置,能上網(wǎng)就能開發(fā)和運(yùn)行
相關(guān)文章
golang DNS服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)操作
這篇文章主要介紹了golang DNS服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
golang實(shí)現(xiàn)對(duì)JavaScript代碼混淆
在Go語(yǔ)言中,你可以使用一些工具來(lái)混淆JavaScript代碼,一個(gè)常用的工具是Terser,它可以用于壓縮和混淆JavaScript代碼,你可以通過(guò)Go語(yǔ)言的`os/exec`包來(lái)調(diào)用Terser工具,本文給通過(guò)一個(gè)簡(jiǎn)單的示例給大家介紹一下,感興趣的朋友可以參考下2024-01-01
淺析Go中函數(shù)的健壯性,panic異常處理和defer機(jī)制
這篇文章主要為大家詳細(xì)介紹了Go中函數(shù)的健壯性,panic異常處理和defer機(jī)制的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-10-10
Go語(yǔ)言fsnotify接口實(shí)現(xiàn)監(jiān)測(cè)文件修改
這篇文章主要為大家介紹了Go語(yǔ)言fsnotify接口實(shí)現(xiàn)監(jiān)測(cè)文件修改的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Go語(yǔ)言基礎(chǔ)結(jié)構(gòu)體用法及示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言基礎(chǔ)結(jié)構(gòu)體的用法及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
一文帶你了解Go語(yǔ)言中的類型斷言和類型轉(zhuǎn)換
在Go中,類型斷言和類型轉(zhuǎn)換是一個(gè)令人困惑的事情,他們似乎都在做同樣的事情。最明顯的不同點(diǎn)是他們具有不同的語(yǔ)法(variable.(type)?vs?type(variable)?)。本文我們就來(lái)深入研究一下二者的區(qū)別2022-09-09

