Where do I start with Go?

If you remember a few weeks ago I was talking a bit about Go, you can watch the video here and I told you about some of Go’s many features.

To start remember that is Go: Go is a lovely little programming language designed by smart people you can trust and continuously improved by a large and growing open-source community.

Go is meant to be simple, but sometimes the conventions can be a little hard to grasp. I’d like to show you how I start all of my Go projects.

Setting up your environment

The first step is, of course, to install Go. You can use the binary distribution for your operating system from the official site. If you use Homebrew on Mac, brew install go works well. When you’re done, this should work:

$ go version

Once installed, the only other thing to do is to set your GOPATH. This is the root directory that will hold all of your Go code and built artifacts. The Go tooling will create 3 subdirectories in your GOPATH: bin, pkg, and src. Some people set it to something like $HOME/go, but I prefer plain $HOME. Make sure it gets exported to your environment. If you use bash, something like this should work:

$ echo 'export GOPATH=$HOME' >> $HOME/.profile
$ source $HOME/.profile
$ go env | grep GOPATH
GOPATH="/Users/kenneth"

If you’re using Windows, don’t forget to add a GOPATH environment variable. With that done, we’re ready to create our first Go application.

In Our first app, we create a file with extension.go in any text editor

$ touch demo.go

Every application in go is commanded by a

package main

Every Go application is made up of packages and, using a Java reference, programs use the main package as the default.

import (
"fmt"
)

As I mentioned at the start of the tutorial, Go has a rather large standard library. To access that functionality, you need to import the specific packages you need.

Here, I’ve imported one:  fmt. If it seems foreign, it really shouldn’t. In PHP and C you’d use includes (or requires), in Python you’d use imports and in Ruby you’d use require.

func main() {
fmt.Println(“Hello  World”);
}

I’ve called the fmt.Println method, which invokes the fmt package that is the data in and out library.

Compiling The Code

Unlike PHP, Ruby, Python etc, Go is a compiled language. So from your project directory, in my case

$ go run demo.go

go run, the only thing it command does is run our file, if we want to compile our demo.go must run go build demo.go

Easy, right?

In Conclusion

We’ve created ‘hello world’  in small steps and using only the Go standard library. Our code can be fetched and deployed on nearly any server architecture. The resulting binary is self-contained and fast. And, most importantly, the code is straightforward to read and reason about. It can easily be maintained and extended, as necessary. I believe all of these properties are a function of Go’s steady and philosophic devotion to simplicity.

If you’d like more information check out the following:

Books

A selection of books about Go.

Tutorials