06. Setup

Before writing any Go, three things need to be on the computer:

  1. The Go toolchain (the program that compiles and runs .go files).
  2. A code editor.
  3. A terminal for typing commands.

This chapter sets up all three on Windows 10 or 11.

1. Install Go

There is more than one way to install Go on Windows. Pick one of the two methods below. If the first fails, try the second.

Method A: winget

Windows 10 and 11 ship with winget, a tool that installs software from the command line. To use it:

  1. Press the Windows key, type PowerShell, and open it.

  2. Type:

     winget search GoLang

    A table appears listing packages whose name or description matches. Look for the package called Go Programming Language. Copy the value in the Id column for that row (it will look something like GoLang.Go).

  3. Install it with:

     winget install --id <paste-the-id-here>

    For example, if the Id is GoLang.Go, the command is winget install --id GoLang.Go.

  4. Close PowerShell and open a new window. This matters: the new window picks up the PATH change the installer made.

  5. Verify the install:

     go version

    You should see a line that starts with go version go1., followed by the rest of the version number and the system, such as windows/amd64. If you see that, Go is installed. Skip to step 2.

If go version prints 'go' is not recognized as the name of a cmdlet, function, script file, or operable program, PowerShell cannot find Go. Check that you opened a new window. If it still fails, use Method B.

Method B: download from go.dev

If winget did not work, install Go manually:

  1. Go to https://go.dev/dl/ and download the installer for Microsoft Windows (the file name ends in .msi).
  2. Run the downloaded installer.
  3. Keep the default choices and click through to the end. The installer adds Go to PATH for you.
  4. Open a new PowerShell window and run go version. You should see the Go version.

2. Install a code editor

Use Visual Studio Code (VS Code for short). It is free, runs on Windows, and is the same editor most professionals use.

  1. Open https://code.visualstudio.com and download the installer.
  2. Run it. On the Select Additional Tasks screen, tick Add to PATH and Register Code as an editor for supported file types.
  3. Open VS Code once the install finishes. If VS Code was already open while you installed Go, close it and open it again so it sees Go.
  4. Install the Go language extension:
    • Click the Extensions icon in the left bar (or press Ctrl + Shift + X).
    • Type go in the search box.
    • Find the extension called Go published by Go Team at Google (the publisher's name is shown under the title).
    • Click Install.
  5. The first time you open a .go file, VS Code may offer to install extra Go tools. Click Install All. It needs an internet connection and takes a minute or two.

This adds syntax colouring, autocomplete, and inline error hints for .go files. The extension also tidies a .go file each time you save it: it fixes the indentation, and it can add or remove import lines. Chapter 08 explains why that matters.

3. The terminal

VS Code has a built in terminal. Open it with **Ctrl + ** (the backtick key, usually under Esc`).

This is where you type go run some-file.go to run a program; its output appears in the same terminal.

4. Verify everything works

Open VS Code and create a working folder:

  1. Choose File → Open Folder..., click New Folder, name it go-work, click Select Folder.

  2. Press Ctrl + N to create a new file.

  3. Type:

     package main
    
     import "fmt"
    
     func main() {
         fmt.Println("Hello, world!")
     }
  4. Save it as hello.go inside the folder (Ctrl + S).

  5. Open the terminal (Ctrl + `) and type:

     go run hello.go

You should see:

Hello, world!

If you do, the whole setup is working. Move on to Chapter 7.

How the example code is organised

All exercise files are in the Exercises section of this book — browse them online or download any file to run it locally.

To get every file at once, download go-101-files.zip, save it in your go-work folder, and type this in the VS Code terminal:

Expand-Archive go-101-files.zip -DestinationPath .

Your go-work folder now holds exercises/ and projects/.

Every chapter with code gets a matching folder under exercises/, numbered to match the chapter: Chapter 7's code is in exercises/07/, Chapter 20's is in exercises/20/, and so on. (Part 1 is all on paper, so the code begins at Chapter 7.) The mini-projects are separate, in projects/.

Each chapter's folder holds three kinds of file:

  • Example files at the top level, like exercises/08/01-comment-styles.go. These are the small programs the chapter's Try this boxes ask you to run.
  • A homework/ folder with a starter file for each homework problem, like exercises/08/homework/01-annotate.go.
  • A homework/solutions/ folder with a worked answer for every homework problem. Try the homework yourself first — the answers are there for when you get stuck or want to check your work.

When a chapter says "open exercises/08/homework/01-annotate.go", find that file in the Exercises section, download it, and save it in the same path inside your go-work folder. Run every command from the go-work folder, which is where the VS Code terminal starts.

Each file is a complete program with its own func main. go run with a file name compiles only that file, so this causes no trouble. VS Code looks at the whole folder, though, so it may underline main in red and say main redeclared in this block. For these exercises you can ignore that. If go run works, the file is fine.

What this chapter did not cover

  • The web. The final part of the book builds a small web server. For now, everything runs as plain text programs in the terminal.
  • Modules. Many Go guides start with go mod init. A single-file program run with go run <file> does not need it. Chapter 25 explains packages and modules.
  • Other editors. Others work fine (Notepad++, Sublime Text), but the book assumes VS Code from here on. With a different editor the keyboard shortcuts change, but the code is identical.