Let’s get started with Golang and creating executable programs

Let’s get started with Golang and creating executable programs

In this tutorial I’m going to show you one way to setup Golang and build your scripts into executable programs(binary files):

First of all you have to go into golang.org, download and install Golang depending on your development platform, in my case I’m using Mac OSX and if you already have installed brew is just as easy as run from console:

brew install go
brew install mercurial
brew install git
//installing godoc for creating package documentation
go get code.google.com/p/go.tools/cmd/godoc

After to install GO you have to declared and export “GOPATH” variable it is recommended to add it inside your user profile maybe if you are using bash you have to put the code into the file ~/.bashrc or in my case ~/.bash_profile or if you are using Zsh into the file ~/.zshrc so the code you have to put into your file looks something like:

#go path
export GOPATH=$HOME/Golang
export PATH=$GOPATH/bin:$PATH
//use the following if you want to generate documentation for your package using godoc
export GOROOT=`go env GOROOT`
export PATH=$PATH:$GOROOT/bin


In reference to the previous code “Golang” indicates the directory where you are going to select as you Go workspace.

In order to load the changes to that file you can close the terminal or just load your bash configuration profile for instance:

source ~/.bash_profile
//source ~/.yourbashconfigurationfile

So don’t forget to create the directory that you set on the GOPATH, in my case I had to create it:

mkdir $HOME/Golang

And also we have to create three more required directories inside our Go Workspace directory:

cd Golang
mkdir bin src pkg

Inside src we are going to put all the code that we write
So let’s create our first program:

cd src
mkdir -p github.com/heridev/hello-world
cd github.com/heridev/hello-worlds

And now inside that directory we are going to create a file called: “hello-world.go”

touch hello-world.go
vi hello-world.go

And inside the file let’s put some Go example code:

package main

import "fmt"

func main() {
  fmt.Println("Hello World")
}

After to add that code let’s build our program hello world to do that we have to run a build command inside our hello-world directory:

go build

Now if we list the files using

ls

We are going to see the following files including the binary file:

hello world in go

hello world in go

And now we can execute our program running from console the binary file:

./hello-world

And then we should see: “Hello World” in the console, and also we can install the script running from console:

go install

We can now run our program with:

hello-world

If the previous code doesn’t work for you try to reload your terminal with the reload command:

reload

And now you are able to run your script in console from anywhere:

hello-world

Additional information:
If you want to lear a little bit more about Go you can take a tour into the language installing and using:

go get code.google.com/p/go-tour/gotour
gotour

I hope you find this tutorial useful :)
H.

No Comments

Post A Comment