Prerequisite learning

Before you start this course, there’s a few things we assume you’ve done:

  • You’re familiar with the essentials of writing code in JavaScript
  • You have experience with JavaScript in the browser and in Node

Preparatory work

Computers and programming

Before attempting any of the projects, you should:

  • Read some of How Computers Really Work by Matthew Justice. You must read chapters 1, 2, 7, 9, 10, 11, 12. Any other chapters may be interesting (feel free to read them! We particularly recommend 8), but aren’t necessary.
    • Note that this book isn’t free - someone at CYF may be able to lend you a copy if you need.
    • You don’t need to remember (or even fully understand) everything you read - the goal is just that you’re familiar with the concepts as you learn more.
  • Complete the Tour of Go
  • Read How to use the fmt package in Golang
  • Learn about pointers and memory:
    • Read the Pointers chapter of An Introduction to Programming in Go, and do the problems listed in the chapter.
    • Read about stack and heap memory - note that the examples here are C++ not Go, but you should be able to follow them. One important difference between these languages is that in Go, you don’t need to worry about whether a value lives on the heap or the stack (in fact, Go is allowed to choose where your variable lives, and even move it around) - this means that it’s ok to return a pointer to a stack variable in Go. You also don’t need to call delete yourself in Go - Go is a garbage collected language so it will delete variables when they’re no longer needed.
  • Read and work through Learn Go with tests up to and including the Mocking section.

This is important because we don’t cover the basic language features of Go: you need to be familiar with writing Go functions and methods, plus the basics of types in Go. You’ll also need to know how to navigate packages and documentation, and we have a short guide on how to do that.

Remember: you can always Google or ask for help if you get stuck.

Linux

Set up and get to know your IDE

We’re going to assume you’re using Visual Studio Code in this course.

VS Code is a lot more powerful that a simple text editor: it can help you write code, spot mistakes, and help you fix them. But it needs to know details of the programming language, so we need to install an extension to support go.

Set up the Go extension for VS Code by following these instructions. Have a read of the features listed on that page.

Some of the really useful ones:

  1. Go to definition - when you call a function or use a variable, this will show you where it was defined. This can help to understand what code is doing and why, and even works when calling into things like the standard library.
  2. Go to references - this will show you what bits of code use a variable or function. Say you’re changing a function to add a new parameter, this can help you find all the places you’ll need to modify.
  3. Autocomplete - Go can guess what you’re about to type, and save you time. But more importantly, it can tell you what exists - if you’re looking to use something related to HTTP, and you think it’s probably in the http package, you can type http. and see what’s auto-completed for you - that could help you find the code you want without needing to switch to Google.

Write a bit of Go in VS Code and experiment with these features. A small investment now will save a lot of time in the future!

Learn how to navigate Go documentation

The Go standard library has lots of documentation and examples, such as net/http. To find documentation, you can use the search feature or Google something like golang net/http, which will generally help you find what you’re looking for.

The website pkg.go.dev also hosts documentation for other go packages that you might use: again you can use the search feature or Google for it.

The structure is fairly similar between different packages. Let’s take fmt as an example:

  • At the start is a summary of the package, discussing what it is for and some important information.
  • The Index lists all the functions that the package exports
  • The Examples show you how to use the package (good place to start!)
  • The Variables, Functions and Types sections contain specific documentation on what the package contains
  • Lastly, Source Files is… the source code! Good Go libraries can be quite readable, so don’t be scared to jump in. You’ll probably learn a lot,

💡 The best way to get familiar with a new package, particularly if the documentation is a bit dense (like for the the fmt package), is to look at the Examples section. It will have some basic and advanced usage that you can often use straight away.

Projects

Complete the first few projects up-to and including “Servers & HTTP requests”.

If you are getting feedback on these projects, we recommend making pull requests (against your fork) early, and get feedback on each before starting the next.

If you have spare time, we also recommend completing the stretch goals of each project before moving on to the next one - it’s better to take time to learn all the lessons from each project, rather than race ahead as fast as you can.

Make sure to read the guidance about the projects below before attempting them.

Reading

Dip in to some longer books, but don’t feel you need to read the whole lot!

Conventions used in projects

Command line examples

In the projects you’ll need to run some programs on the command line. To show this, and the output, we’ll use an example like the following:

> echo "Hello, world."
Hello, world.

What we mean is: “run the command to the right of the > sign”: echo "Hello, world."

Everything after that line is output from the command: Hello, world.

In the above example, the command is echo with the argument "Hello, world.". Here’s a more complex example using curl:

> curl -i 'http://localhost:8080/'
HTTP/1.1 200 OK
Content-Type: text/html
Date: Sun, 24 Jul 2022 09:42:30 GMT
Content-Length: 42

<!DOCTYPE html><html><em>Hello, world</em>

It doesn’t matter what this does: what’s important is the input command and the expected output.

Input command:

> curl -i 'http://localhost:8080/'

Expected output:

HTTP/1.1 200 OK
Content-Type: text/html
Date: Sun, 24 Jul 2022 09:42:30 GMT
Content-Length: 42

<!DOCTYPE html><html><em>Hello, world</em>

Important: the output from commands that you run will often not be identical to the example. Dates, times and counts will be different.

Sometimes we may put more than one command in the same snippet:

> echo hello
hello
> echo goodbye
goodbye

Generally each time a line starts with a >, it’s a new command (but occasionally it may be output from a previous one!)

Working on the projects

To work on the projects:

In general, work through each project like this:

  1. Make it work
  2. Make it right
  3. Optional: Make it pretty (or fast)

Making it “right” means ensuring the structure is clear, reasonably simple, and documented, with error handling and tests in place. But it does’t mean perfect!

In other words, resist the temptation to optimise or refactor individual pieces of the project, for example to use fancy techniques like go-routines and channels, until you have project working end-to-end. Sometimes it’s only when you have the whole working project in front of you that you can see how the individual pieces should really work, and optimising too early can waste time.

Opening the right directory

When writing Go in VS Code, many of the helper features only work if you opened the folder directly containing the file named go.mod.

So when working in immersive-go-course, you need to open a new window for project directory with the code you’re working on.

If you opened VS Code in the main (“root”) directory of immersive-go-course, you’ll probably see a lot of red squiggly lines in your Go code and errors starting “could not import”.

You can have more than one copy of VS Code open at a time if you need to.