Skip to content

Latest commit

 

History

History
137 lines (88 loc) · 7.09 KB

README.md

File metadata and controls

137 lines (88 loc) · 7.09 KB

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
  • You've completed the Tour of Go

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.

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.

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.