Introduction to Libraries in Go

The world of software development is an ice berg. There is always more below the surface. When I started into programming, I didn’t know enough to not know what I didn’t know. But don’t let that get too overwhelming for you. As developers, we can stand on the shoulders of amazing developers that have come before us. How do we do this? Well by leveraging libraries, specifically libraries provided under an Open Source License.


Feb. 21, 2024 727 words tools · beginner ·

The world of software development is an ice berg. There is always more below the surface. When I started into programming, I didn’t know enough to not know what I didn’t know. But don’t let that get too overwhelming for you. As developers, we can stand on the shoulders of amazing developers that have come before us.

How do we do this? Well by leveraging libraries, specifically libraries provided under an Open Source License.

You can definitely follow the link above to learn more about open source overall. However, in general (as it does get complex there are a number of open source licenses out there), someone (or a group) writes and provides usage of code free of charge. You can then pull this code into your project to get functionality that would be cost, time, or skill prohibitive to create on your own.

How do I do this? Fortunately, go has a great use case for this, it is built into the compiler tool-chain that you installed (you did install the go tools right?). Most other languages require you to install yet another tool to start accessing libraries, but with go you have everything you need.

Previously I talked about getting accustomed to using the command line interface. This is another case where that is very important.

Since we’re going to be developing games, lets look at a library that is the game engine that we’ll be using. (There are many in go, but I going to be talking about one that I like for its simplicity). Ebitengine is the library that we’ll be using. Go ahead and check it out, but be sure to come back.

Once you’ve created your project, you’ll be prepared to follow along with the rest of our discussion here. So, if you haven’t created one yet, get going.

Once you’ve got your project created, you’ll need to make sure that you’re command line interface is in the project directory. You can usually do this like this:

cd {projectName} # The curly braces, they denote a placeholder. Use the name you picked.

Now that you’re in the right directory you’ll need to run another command to pull down the source code for the Ebitengine library.

go get -u github.com/hajimehoshi/ebiten/v2@latest

The command

So, What Happens?

This downloads the library code located at the url to your local machine. It is put in a source directory that the go tool-chain understands. Then, you have access to all of the functions, methods, and other pieces of code that this library provides. When it comes time to compile, the compiler will compile these sources with your project.

Let’s look at some code that works with the library. This code is pulled directly from the install instructions.

package main

import (
	"log"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)

type Game struct{}

func (g *Game) Update() error {
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	ebitenutil.DebugPrint(screen, "Hello, World!")
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 320, 240
}

func main() {
	ebiten.SetWindowSize(640, 480)
	ebiten.SetWindowTitle("Hello, World!")

	// This ebiten.RunGame takes an interface, remember a collection of methods.
	if err := ebiten.RunGame(&Game{}); err != nil {
		log.Fatal(err)
	}
}

We’re not going to discuss the code so much, as look at the import block. There are three packages there log (which is part of the standard library), and then two ebitengine packages.

Then, inside of the main function, you’ll see some methods being called on the ebiten package. These setup the game window, but we’ll go into more detail later.

Wrap It Up

I realize that this was a short high level discussion about libraries, often it isn’t until you’ve written a bunch of code, that you understand their use. However, if you’ve followed along so far, you should be able to start running a basic game window.