The code for this tutorial is available here.
Introduction #
In this tutorial we will set up a development environment and write our first Go program.
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 install requirements.
Download the Go compiler at https://go.dev/dl under “Stable versions”. You will need to select the appropriate download for your operating system:
- Linux: download
go-linux-amd64.tar.gz
. - Windows: download
go-windows-amd64.msi
. - MacOS: download
go-darwin-YOUR-CPU.pkg
.
Installation instructions differ depending on operating system. Click here to view the installation instructions for your system.
Once you have followed the installation instructions for your system, 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
Initialize a Go Module #
Now that we have the Go compiler installed, 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.
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, which
is 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.
Stay tuned for the next tutorial, Getting Started with Ebitengine.
Please consider donating if you found this tutorial helpful.