Codular

HomeWriters RSS

Getting Started with Go

Introduction

Sometimes referred to as 'Golang', Go has been around for a while now - with regular updates and a strong development push currently underway to change how the core of the compiler works. No need to dwell on that.

There are many benefits of using Go, two of the many are:

  1. No dependencies
    You can compile the code into the binary for your operating system, and then run it - there is no need to preinstall anything on your server like with PHP, or Ruby.
  2. Concurrency
    You can quickly, and easily run multiple threads, or channels, of code as the whole concept is baked directly into the core of Go.

Installation

You can of course use the download page from Go to install either through compiling, or downloading a package. However, a very helpful tool is something called "Go Version Manager" or "GVM" - available on GitHub a simple bash command will install the manager. Allowing you to install a specific version - very handy if you want to compile your code against a new version without breaking the ability to build to an older version for hotfixes if needed.

Remember, version control is great due to the fact that you compile a dependency - you can retain the binary file that is generated for every build so that you can easily rollback if needed.

This Example

For this example we'll use the latest version of Go that is available today - 1.4 - after installing gvm we can install this by running:

gvm install go1.4

When this is installed you should be able to run the following command to set the default version to be 1.4.1:

gvm use go1.4 --default

If this has all ran successfully, you should be able to just run the command go, which should output a list of available commands. If so, we're ready to go with the next section!

Hello World

There's nothing worse than starting with Hello World - but the idea here is to get the basic idea of compiling the language under control, so that future projects can easily be developed and compiled.

The code for this is pretty straight forward, below is the whole code, and after it, ane explanation of each section.

package main

import "fmt"

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

All code needs to be assigned to a package - this is useful if you're building a library to allow others to use, or if you want to split out your code. Here, we're simply making it part of the main package.

Next, we need to import the package for us to output text to the screen - the fmt package manages everything to do with I/O. We do this using the import command with the package name after it, if we were importing multiple packages we would do something like this:

import (
    "fmt"
    "os"
    "os/exec"
    "time"
)

In our Go code, the function with the name main always gets called first - so our code needs to go in there - we can call other functions from within that without an issue. But everying originates within that function.

Finally, we use the Println method within the fmt package to print out some text. This method outputs a new line after the text that was entered.

Compiling

When you've written the above code, you're ready to start compiling the code.

If you've used non-standard packages such as ones directly from Github or other locations, you will need to make sure that you install the packages first - this is done using the command:

go get

As we're using core packages, we don't need to run that, so we can go ahead and just run the build command as follows:

go build

This will build the binary of your code - you should be able to go ahead and run that now. If you run it you should see 'Hello World' outputed to the console like so:

$ ./hello
Hello World
$

Directory Structure

When you build larger scale applications, you'll need to start setting the GOPATH, and structuring your directories correctly. You basically need three directories within your main project directory, these are:

For our example we're going to go with this directory structure, with our code going into hello_world.go:

Now what we would do, is set the GOPATH to be the root hello_world directory. There's a super quick way to do this in terminal - make sure you're in that directory and then run the following command:

GOPATH=`pwd`

That will print the currect directory, and take that string to be the GOPATH - you can check it's set correctly by running echo $GOPATH.

Next, when you want to run go build, you just simply move into the hello_world/src/hello_world directory, and as previously just run go build. As simple as that.

Conclusion

A super quick example about how to get your Go system set up, and running - with a working directory setup and structured correctly. In the future, we'll look at how to push the language a little further, and how to use third party libraries as well including Martini - a super simple web development framework. More on that in the future.

Tag: Golang