2. Getting Started with Go

2. Getting Started with Go

Watch the associated video on YouTube or MakerTube

Watch on YouTube

Introduction #

In this tutorial we will install the Go compiler, set up our development environment and write our first Go program.

The code for this tutorial is available here. Please consider donating if you find this tutorial helpful.

The Go Programming Language #

Go is a general purpose programming language created in 2007 by engineers at Google.

Go programs are statically typed and are compiled to bytecode. Upcoming tutorials will explain this further.

Go programs may be compiled to run on many platforms, including Linux, Android, Windows and WebAssembly.

Install the Go Compiler #

In order to write games in the Go programming language, we need the Go compiler. The Go compiler has the responsibility of translating our Go source code files into executable files we can distribute. Only the person compiling a Go program needs the Go compiler. Once a Go program is compiled into an executable, the executable file may be distributed to others without any extra installation requirements.

Download the Go compiler at https://go.dev/dl under “Stable versions”. You will need to select the appropriate download based on your operating system and CPU architecture. Choose amd64 if you have an AMD or Intel processor.

  • Linux: download go-linux-amd64.tar.gz.
  • MacOS: download go-darwin-amd64.pkg.
  • Windows: download go-windows-amd64.msi.

Installation instructions differ depending on your operating system and how you choose to install Go.

If you are using Linux, you may wish to install Go using your system’s built-in package manager instead.

On Debian Linux, you can install Go by executing the following command:

sudo apt install golang

Verify Go is Installed #

Open a terminal and execute the following command:

go version

You should see something similar to the following:

trevor@eros ~ $ go version
go version go1.21.1 linux/amd64

You may have a different version of the Go compiler installed.

Initialize a Go Module #

Now that we have installed the Go compiler, we can start writing some code.

Go modules are used organize Go source code. Create a folder just for this tutorial. Open a terminal, cd to the folder you just created and then execute the following command:

go mod init gettingstarted

This will create a file named go.mod which tells Go what your module is named (gettingstarted), the minimum version of Go required and a list of the dependencies of your module.

We don’t have any dependencies yet, this program will only use the Go standard library, which is always available. We will add our first dependency in the next tutorial.

Create main.go #

While there is no rigid structural requirement for projects written in Go, there are conventions which are commonly followed. This makes it much easier to read and understand code belonging to projects you are not familiar with.

One such convention is creating a main.go file containing the main function, where the program begins execution.

Create a file named main.go and add the following:

package main

import "fmt"

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

After saving the file, run the following command:

go build

After Go finishes compiling your program, you should have an executable named gettingstarted.

Execute gettingstarted inside a terminal, and you should see a greeting. Try changing the text from “Hello, world!” to something else, being careful to keep the quotation marks. Run the go build command and then gettingstarted again. You should see your updated message.

While go build is used to build and distribute your program to others, there is another command that makes it easier to test your changes quickly. Execute go run instead to immediately run your updated program.

IDE #

Installing the Go extension in VSCodium

It is common practice to use an IDE when developing software. Most feature powerful tools which improve productivity.

The following tutorials assume the Go compiler and an IDE are installed. If you don’t have an IDE installed yet, VSCodium is recommended. It is free, open source and easy to setup. There are many other free and open source IDEs as well. Any IDE with support for writing Go code will work, and you should use whichever IDE you feel most comfortable with.

By default, VSCodium does not include support for the Go programming language. Once you have installed VSCodium, open it and click File > Preferences > Extensions. Search for “golang”, and one of the results will be “Go - Rich Go language support for Visual Studio Code”. The author of the extension is “golang”. Install this extension to enable support for the Go programming language.

Once the extension is installed, you will be prompted to install a few additional tools. These tools handle tasks such as analyzing Go code for errors or other problems before the code is compiled.


Click here to proceed to the next tutorial, Getting Started with Ebitengine.

Please consider donating if you found this tutorial helpful.