国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Golang > 正文

linux中用shell快速安裝配置Go語言的開發(fā)環(huán)境

2020-04-01 19:08:04
字體:
供稿:網(wǎng)友

介紹

go1.5+版本提供編譯好的安裝包,我們只需要解壓到相應(yīng)的目錄,并添加一些環(huán)境變量的配置即可。

Go語言的安裝步驟

     下載安裝包go1.7.linux-amd64.tar.gz

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

     設(shè)置環(huán)境變量:GOROOT, GOPATH, PATH

既然我們可以列出這些步驟,那么便可以將整個(gè)過程自動(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.gzfunction 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 1fifile=$1if [ ! -f $file ];then error "${file} not exist..." exit 1fiunzipPath="`pwd`/tmp_unzip_path/"info $unzipPathif [ ! -d $unzipPath ];then info "not exist" mkdir $unzipPathfitar -zxf $file -C $unzipPathgoroot=$GOROOTif [ ! -n $GOROOT ];then warn "Use default go root /usr/local/go" goroot="/usr/local/go"figopath=$2info "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 fificreateGoPath $gopathinfo "Copy go unzip files to $goroot"sudo cp -r "$unzipPath/go" $gorootrm -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/nullfiecho $exportGoroot | sudo tee -a $etcProfileexportGopath="export GOROOT=$gopath"if [ ! -z $GOPATH ];then cat $etcProfile | sed 's/^export.GOPATH.*//' | sudo tee $etcProfile > /dev/nullfiecho "export GOPATH=$gopath" | sudo tee -a $etcProfileecho 'export PATH=$GOROOT/bin:$GOPATH/bin:$PATH' | sudo tee -a $etcProfile# ## Replace multiple empty lines with one empty linecat $etcProfile -s | sudo tee $etcProfile > /dev/nullinfo "To make configuration take effect, will reboot, pls enter[y/n]"read -p "[y/n]" isRebootif [ $isReboot = "y" ];then sudo rebootfi

講一講腳本做的事情吧

     1、腳本要求輸入編譯好的安裝包,這里本來是可以做成直接下載的, 但是考慮到大多數(shù)人是無法連接到golang的官網(wǎng)的,因此放棄了這一步。

     2、解壓文件到指定的目錄, 默認(rèn)為/usr/local/go , 也可以通過運(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)配置過golang的環(huán)境變量, 那么需要先刪除這些配置,然后重新寫入。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芦山县| 渭南市| 修水县| 来安县| 浪卡子县| 永丰县| 库伦旗| 尚义县| 蒙山县| 延津县| 凌云县| 苍梧县| 胶州市| 莱西市| 深州市| 故城县| 茂名市| 崇义县| 建水县| 郯城县| 友谊县| 青海省| 南漳县| 张家口市| 安丘市| 来安县| 明溪县| 桂东县| 渝北区| 格尔木市| 宁阳县| 全椒县| 南和县| 花莲市| 沙坪坝区| 永城市| 鲁甸县| 庆元县| 绥宁县| 南雄市| 石家庄市|