Learn to program Go Tutorial One

This is the first in a series of tutorials that teaches you to program in Google's Go. This is for anyone who has done some programming and understands the basic concepts like variables, if statements etc. You certainly don't have to be an expert but if you want to learn programming from scratch this probably isn't the best tutorial.

What is Go?

Started in 2009 by Google and released at version 1.0 in 2012, Go is a compiled.

garbage collected concurrent programming language. It's statically compiled (like C, C++, C#, Java), compiles very quickly and has a few similarities with C, while being as general purpose as C++.

The method of teaching is by example with lots of smaller examples showing how a particular language feature is used and explaining it.

Windows, Linux or Mac?

Go was originally developed on a Linux platform but is platform neutral with versions for each platform.

Developing Go Programs

Currently there is no best IDE for Go. For WIndows, Linux or Mac OSX. There are two free ones:

  1. golangide an open source IDE written in C++.
  2. Alternatively if you know Eclipse there is a plugin for that (for Windows or Mac OS X, not Linux) called goclipse with syntax highlighting, autocomplete, error reporting in Eclipse.

For Windows users (and Ubuntu under Wine) there is the commercial Zeus Go Language IDE which costs about $99.

I've setup Eclipse with goclipse to use for my Go development system but it's perfectly ok to just use a text editor and the command line go compilers.

These tutorials do not require anything else except to have Go installed. For that you should visit the official Go website and follow their instructions.

The source code for the three example Go programs below are in this zip file.

  • Download tutorial1.zip.

So let's get started with the tutorial. Until we come on to using packages, assume the program is in a single text file with the extension .go. The three examples provided here are ex1.go, ex2.go and ex3.go.

Comments in Go

These are the same as in C++ and C99. Single lines use // and multi lines start with /* and end with */.


// A single line comment in Go
/* This Go comment
is spread over
three lines */


Hello World

It's a tradition to start with a Hello World program, so here it is, probably the shortest working Go program you can have.


package main

import "fmt"

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


Compiling and Running Hello World in Go

Unless you do it from a Gui, (My Eclipse/goclipse is set to build automatically and I click a green arrow to run it), from a command line (terminal in Linux), you run it with the


go run hello.go


This both compiles and runs it.

Let's examine the structure of the program. Go's code can be split into logical groupings called packages and these export methods and fields that are imported by other packages.

In thie program the "fmt" package is imported to provide access to the fmt.Println() function.

This package provides input and output functions similar to scanf and printf in C.

The fmt package proves formatted input and output with 19 functions. fmt.Println() outputs the specified string. Half way down that page you can see all 19 functions and six types that are exported by "fmt" and available to use.

The use of packages and restricting what is exported and imported in other packages is what make Go so powerful and compinling so fast. As well as the standard packages there is a growing list of third party provided ones.

Program Structure

The main func is not imported, it has no arguments and returns no value but it has to be present for a complete program to be created.

Use of Semicolons

Compared to C there are only a few places (e.g. in a for statement) where these are needed. The compiler inserts them between tokens but you never see those.

This keeps the syntax cleaner and easier to read and understand.

Variable Declaration and Example 2

Remove everything inside the func function in the example earlier and replace it with this:


var a,b int
var c int

a =10



This declares three int variables a,b and c. If you're used to C/C++/C#, the order of declarations is the reverse and you don't need the var keyword.

I could have declared them all on one line with var a,b,c int but this shows it's flexible.

After the declaration Then a and b are assigned values and c is assigned the total of a + b. Finally the fmt.Println(c) outputs the value of c and you see 17.

Example 3

There's another way to declare a variable using := which assigns an initial value and determines the type of the variable. So you don't need the var. Here's that last example rewritten (and I changed the value of to 8).


var c int

a := 10
b := 8



a := 10 declares a to be of the same type as the rhs of the := (10 so therefore int). Any rhs that is all digits 0-9 and starts with 1-9 (base 10 decimal), 0 (base 8 octal) or 0x (base 16 hexadecimal, 0X is also valid) is an int.

So these are all equivalent:

a := 10 // decimal
a := 012 // octal = 1x8 + 2 = 10
a := 0xa // hexadecimal a=10

In tutorial two, I'll continue with declarations including floats and strings. See Google Go tutorial on variable declarations.