Some Go basics to get you started, Part I

Go is a very simple programming language, with surprising power. Some of its main intentions are:

  1. to be easy to read.
  2. to be easy to write.
  3. to provide concurrency (the Go language has a go keyword for this).

This last point, concurrency, I hardly use in most of what I do. I am not trying to optimise code to the _n_th degree. I am solving issues I experience in my daily work so that when it becomes someone else’s problem, it’s already solved. If I use concurrency, it’s improving a very specific issue and I really need to reason about why I’m doing it.

The Obligatory Hello World Example

Most of us are familiar with the “hello world” classic right? Here’s what it looks like in Go:

package main

import "fmt"

func main() {
	fmt.Println("Hello, world.")

There are a few things going on here. There’s some package thing at the top, some import thing next line down, some func thing with a main word in it, then a line of code that appears to print something to somewhere. Let’s break it down.

The package main line right at the top, and this is always the first line of code in your file, defines the name of the package that your file belongs to. What is a package? Put simply, it’s a little “box” where functions and types sit to help separate code in your application, like namespaces in other languages. If everything was in one package, your application would get messy.

We always define the main package as the root of your project. It does not matter whether you call your project “git” or “copy” or “my-awesome-tool”, there is always a main package in the root. There is also always a main function in an application (Go libraries are different and we’ll get to those later). To create the simplest application possible the listing above is all you need.

The import "fmt" line of code tells your application that it needs a standard library package called “fmt”. Doing this import means that you get access to all the accessible types and methods from the fmt package. It’s not important what is in this package right now.

The func main() { line defines what we call a function. In this application, it’s also the entry point since it’s called main. This is a common design borrowed from C. Every application that you write will have a main function somewhere, always in the root, and most often (and recommended) in a file called main.go since that is easy to find. This main function is the first thing called when your application launches (not entirely true, there’s also a special init function for each package that’s called first, we’ll cover this later. For now, just know that main is your entry point).

The fmt.Println("Hello, World.") line really says “call the function Println from a package called fmt, and pass it "Hello, World." as the first parameter. It is not calling a static method on a class called fmt, which is how I read this when I first read Go code, and only because I thought it was object oriented before I’d actually looked at it. If you look at the code comments for fmt.Println it prints to the standard output, which by default means your console screen.

I’ve skimmed over a lot of concepts above. Packages, functions, and parameters. I will dive a little deeper in to each one in time.