December 7, 2024

Go language howto

Go language is a thing built by Google that looks like C/C++ but simpler. It’s also sorta like Java, but built for the web, and sort of optimizing those other languages to be simpler to write stuff.

Install go on Mac using macports

su
port install go

This puts your go stuff in /opt/local/lib/go/, but then you probably want to write go code as a normal user, so add a folder in your home directory where you’re going to put your go code like:

mkdir ~/whatevergofolderyouwanttocallit

Now edit your .bash_profile file and add:

nano ~/.bash_profile
  export GOROOT=/opt/local/lib/go
  export PATH=$PATH:$GOROOT/bin
  export GOPATH=$HOME/whateveryoucalledyourgocodefolder
  export PATH=$PATH:$GOPATH/bin

Now exit your terminal, or just open a new tab and you should be set to go.

Install go on Debian Jessie

apt-get install golang

Now set up your normal user to be able to write code in your home folder but run it from where the go binaries are like:

su
mkdir /usr/lib/go/bin
ln -s /usr/bin/go /usr/lib/go/bin/go
vi ~/.bashrc
  export GOROOT=/usr/lib/go
  export PATH=$PATH:$GOROOT/bin
  export GOPATH=$HOME/whereveryourworkspacefolderwillbe/workspace/go
  export PATH=$PATH:$GOPATH/bin

now you have to logout of your terminal and start another one to read your paths. Once you do this, you can start writing and running Go code.

Install on Debian Jessie from source

If you want the latest version of golang, you’ll have to download it from the source code repository here, then do:

cd /whereveryoudownloadedit/
tar -C /usr/local -xzf go1.5.2.linux-amd64.tar.gz (match your version)
export PATH=$PATH:/usr/local/go/bin

Now make update your shell so it will recognize the path:

vi ~/.bashrc
  export GOROOT=/usr/local/go
  export PATH=$PATH:$GOROOT/bin
  export GOPATH=$HOME/whereveryourworkspacefolderwillbe/workspace/go
  export PATH=$PATH:$GOPATH/bin

Now exit your terminal and log back in. If it worked, run:

#>: go version
go version go1.5.2 linux/amd64

If you don’t see the version number, fix that before you proceed, or stuff won’t work.

Setting up Go for Eclipse/GoClipse

You probably want to write go code with Eclipse (but really you could write it with vi or nano) so you should probably install that. After you get Eclipse installed, you have to install GoClipse like by going to Help > Install New Software > Add > Location: http://goclipse.github.io/releases/ > Ok (it will show Pending for a little bit, then show) > Trust Certificate Brainwy (Select) > Ok > Prompt for Eclipse restart.

On Debian Jessie, you now have to open Eclipse/Goclipse and specify a workspace, so pick:

/home/youruser/whereveryourworkspacefolderis/workspace/go

Now you have to create a new Go project using File > New > Go project > type somename > then UNCHECK USE DEFAULT LOCATION and browse to:

/home/youruser/whereveryourworkspacefolderis/go/src/

and click Finish.

Now you have to create a new go file to put your code in by selecting your project file in the Project Explorer, then right-clicking and doing New > Go file like:
new_go_file
In this new file (I called mine dbaccess.go), just start with some sample code, like (cut/paste next part):

package main
 
import "fmt"
 
func main() {
	fmt.Println("Hey, it worked!")
}

Now click File > Save all

then click the play button, and in the console you should see “Hey, it worked!”

Congratulations, you have just made your first Go program!