linux USES shell to quickly install and configure the Go development environment

  • 2020-06-03 06:43:27
  • OfStack

introduce

The es3EN1.5 + version provides a compiled installation package. All we need to do is unzip to the appropriate directory and add the configuration of 1 environment variable.

Steps to install the Go language

Download installation package go1.7.linux-amd64.tar.gz

Unzip the file to the specified directory: tar -zxf go1.7.linux-amd64.tar.gz

Setting environment variables: GOROOT , GOPATH , PATH

Now that we can list these steps, we can automate the entire process.

Here is the installation script


#!/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

So let's talk about what the script does

1. The script requires the input of the compiled installation package, which could have been downloaded directly. However, considering that most people cannot connect to golang's official website, this step is abandoned.

2, unzip the file to the specified directory, default is /usr/local/go Can also be specified at runtime

3. Create three folders under GOPATH: src, bin, pkg, GOPATH can be specified at runtime, default is /home/{user}/workspace/golang

4. Setting environment variables: $GOPATH , $GOROOT

5. Restart the service to make it correct /etc/profile Take effect

Here are a few ideas: It is possible that the system has configured golang's environment variables, so you need to delete these configurations and then rewrite them.

conclusion

The above is the whole content of this article, I hope the content of this article can bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.


Related articles: