Jump to content
Aerosol

Simple bash script for setting up Golang environment

Recommended Posts

Posted

Some time ago, when i just started to use golang, golang project's structure organizing, this magic $GOPATH and other things was strange for me. No, it wasn't bad, just nothing like i have used before. It's simple to understand with erlang, at least for the start, you must just install erlang, to create project, to add rebar with deps and that's all. Something like this with nodejs project bootstraping. There are clear and intuitive package managers which downloads project's dependencies and puts it to the project dir.

Another case with Golang && go get. They are very cool, but not for the first look. You read it's documentation, execute all instructions and than get something like go can't find `projectname` in $GOPATH and $GOROOT. So i created simple bash script which will help you to set up golang environment from the scratch. It makes 3 simple things:

  • Creates directories for Golang compiler and your future projects
  • Downloads golang compiler and builds it
  • Puts golang/bin/ and directory with your projects to $GOPATH

That's all. Very simple. Of course this script is not for a professional golang developers. It's primarily for two points:

  • For newbie gophers
  • For fast setting up golang environment

Here is the script:

#!/bin/bash

#
# Bash script for fast setting up Golang environment
#
# Usage:
#
# sudo chmod +x gostartup && ./gostartup
#

red="\e[31m"
green="\e[32m"
nocolor="\e[0m"

bashrc="/home/"$USER"/.bashrc"

echo -e "Where do you want to store golang projects [default: /home/$USER/go] \c:"
read gopath

if [[ $gopath == "" ]]
then
gopath="/home/$USER/go/"
fi

if [[ "${gopath: -1}" != '/' ]];
then
gopath=$gopath"/"
fi

gobin=$gopath"go"
projects=$gopath"go-projects"
struct=$gopath"go-projects/src/github.com"$github_user

echo -e "What's you Github username: \c'"
read github_user

if [[ $github_user == "" ]]
then
echo -e $red"[Error]: Github username can't be empty"$nocolor
exit 0
fi

echo -e "Do you want to get last golang compiler [y/n]: \c"
read getgo

if [ ! -d "$gopath" ]; then
mkdir $gopath
fi

if [ ! -d "$gobin" ]; then
mkdir $gopath"go"
fi

if [ ! -d "$projects" ]; then
mkdir $gopath"go-projects"
fi

if [ ! -d "$struct" ]; then
mkdir -p $gopath"go-projects/src/github.com/"$github_user
fi

if [[ "$getgo" == "y" ]];
then
hg clone -u release "https://code.google.com/p/go" $gopath"go"
exec $gopath"go/src/all.bash"
fi

echo "export GOPATH="$gopath"go-projects" >> $bashrc
echo "export PATH=$PATH:$GOPATH/"$gopath"go/bin" >> $bashrc

echo -e $green"done" $nocolor

exit 0

Source

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...