Thuta Learning
Go
BasicProgrammingBeginner

Get Started (Setup)

GoLesson 325 min

What you'll walk away with

  • Install Go and confirm it with go version in a fresh terminal
  • Create a module with go mod init and read what go.mod contains
  • Explain the difference between go run ., go run main.go and go build
  • Fix setup errors such as 'go is not recognized' and 'go.mod file not found' yourself

Explain it simply

Setup, simply

To run Go code on your own computer you need the Go toolchain: the compiler, the standard library and the `go` command-line tool, packaged together. Once it is installed, `go run` runs code directly and `go build` produces an executable file.

Each project lives in a folder called a module. The module's ID card is the `go.mod` file, which records the project's name and the Go version it uses. You create that file once per project, with a single command.

Problem → Why → Solution

The problem: the code is fine but it will not run

Most setup problems have nothing to do with code. If the terminal is in your home folder and `main.go` is inside a `hello` folder, `go run main.go` simply cannot find the file. Move into the right folder, try `go run .`, and a different error appears because there is no `go.mod` yet.

If you have never seen these messages, it is easy to assume the code is wrong and start changing code that was correct all along.

terminal
C:\Users\aye> go run main.go
CreateFile main.go: The system cannot find the file specified.

C:\Users\aye> cd hello
C:\Users\aye\hello> go run .
go: go.mod file not found in current directory or any parent directory;
see 'go help modules'

Why these errors happen

The `go` command looks for files and for `go.mod` starting from the terminal's current folder. The first error means the file is not in the current folder (macOS and Linux say `stat main.go: no such file or directory`). The second means that running `.`, the package in this folder, needs a module, and there is no `go.mod` yet. One `go mod init` fixes it.

Core concept: from install to first build

Setup is done once and has five steps. Check the result of each step before moving on, and when something goes wrong you will know immediately which step caused it.

1. Install

Download the installer for your OS from the official site go.dev/dl (.msi on Windows, .pkg on macOS, .tar.gz on Linux). The installer adds `go` to your PATH.

2. Verify in a new terminal

Open a new terminal and run `go version`. You should see one line with the version, OS and CPU architecture, such as `go version go1.24.4 windows/amd64`.

3. Start a module

Inside the project folder, run `go mod init example.com/hello`. This creates `go.mod`. The module path can be any name, often a repository address such as github.com/you/hello, but it must not contain spaces.

4. Run

Write `main.go` and run it with `go run .`, where `.` means 'the whole package in this folder'.

5. Build

`go build` writes an executable named after the last part of the module path: `hello.exe` on Windows, `hello` on macOS and Linux.

go.mod
module example.com/hello

go 1.24.4

`go.mod` holds the module path and a `go` line. The `go` line records the toolchain version you created the module with and states the minimum Go version the project needs, so your number may differ. Later, when you use third-party packages, `require` lines and a `go.sum` file appear. Modern Go uses modules by default, so your code no longer has to live in a special GOPATH folder.

When you want to know where things live, ask `go env`. `go env GOROOT` prints the folder where Go itself is installed, and `go env GOPATH` prints the folder that caches downloaded module source and holds tools added with `go install`; by default it is a `go` folder in your home directory. You rarely change these values, but if a tool installed with `go install` will not run, the `bin` folder inside GOPATH usually needs to be on your PATH.

Mental model: four checkpoints
  1. Does go version work?
  2. Am I in the right folder?
  3. Does go.mod exist?
  4. Does go run . work?

When a setup error appears, walk these checkpoints from the top; most problems are in the first three.

General pattern: starting a new project

bash
mkdir project-name
cd project-name
go mod init example.com/project-name
# write main.go with package main and func main()
go run .
go build

Create a folder, move into it and initialize the module. Then write `main.go`, try it with `go run .`, and use `go build` when you need an executable. Follow this order for every new project.

The first main.go inside a module

In the folder where you ran `go mod init example.com/hello`, write this `main.go` and run it with `go run .`. It prints the setup steps with numbers. Slices and `for` loops get their own lessons later; for now read the loop as 'print one line for each step'.

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    steps := []string{"go version", "go mod init", "go run .", "go build"}

    fmt.Println("Setup checklist:")
    for i, step := range steps {
        fmt.Printf("%d. %s\n", i+1, step)
    }
    fmt.Println(strings.Repeat("=", 16))
    fmt.Println("Toolchain ready!")
}

Code breakdown

  1. Two standard packages"strings"

    Both `fmt` and `strings` are part of the standard library, so nothing is downloaded and no `require` line appears in `go.mod`.

  2. A list of stepssteps := []string{"go version", "go mod init", "go run .", "go build"}

    `[]string` is a list (a slice) of strings. It holds the four commands in order.

  3. Numbering each stepfor i, step := range steps {

    `range` hands you each item's index, starting at 0, and its value. That is why the next line prints `i+1`, so numbering starts at 1.

  4. A separator linefmt.Println(strings.Repeat("=", 16))

    `strings.Repeat` returns `=` repeated 16 times. This line is the only use of `strings`; delete it and the import becomes an unused-import error.

You should see
Setup checklist:
1. go version
2. go mod init
3. go run .
4. go build
================
Toolchain ready!

go run main.go versus go run .

As a project grows, the `main` package is often split across several files. `go run main.go` compiles only the file you name, so calling a function from `helpers.go` fails with an `undefined` error. `go run .` compiles every `.go` file in the folder as one package. Make `go run .` your habit inside a module.

CommandWhat it does
go versionShows the installed version
go mod init pathCreates a new module with go.mod
go run .Compiles and runs the package in this folder
go buildWrites an executable into the folder
go fmt ./...Formats every file the gofmt way
go vet ./...Finds suspicious code that still compiles

In real projects

How teams handle setup in practice

On a team project, a new developer should be able to clone the repository, run `go build ./...` and get a working build. `go.mod` and `go.sum` are committed to git, so everyone uses the same dependency versions. In Go 1.21 and later, the `go` command can even download the toolchain a project asks for through the `go` and `toolchain` lines in `go.mod`.

CI pipelines run the same commands you learn here, in order: `go vet ./...`, `go test ./...`, then `go build`. Building for a Linux server only takes two environment variables, as in `GOOS=linux GOARCH=amd64 go build`. Understanding setup on your own machine means you already understand most of a production build.

Common mistakes

Common setup mistakes

1. Using an old terminal after installing

A terminal opened before the install does not know the new PATH, so Windows says `'go' is not recognized as an internal or external command` and macOS or Linux say `go: command not found`. Close it and open a new one; for VS Code's built-in terminal, restart VS Code.

2. Running from the wrong folder

`go run main.go` only looks in the current folder. Before running, list the folder with `dir` on Windows or `ls` on macOS and Linux; if `main.go` is not there, `cd` into the project folder.

3. Running go run . without a go.mod

`go run .` and `go build` are meant to work inside a module, so without `go.mod` they stop with `go.mod file not found`. Run `go mod init` once in every project folder; running it a second time in the same folder is refused because go.mod already exists.

Mini challenge

Mini challenge: a setup report

Create a new module called `setupcheck` and write a program that prints your Go version, operating system, CPU architecture and number of CPU cores.

  • Run `go mod init example.com/setupcheck` in a new folder
  • Use the `runtime` package from the standard library
  • Print `runtime.Version()`, `runtime.GOOS`, `runtime.GOARCH` and `runtime.NumCPU()`
  • Run it with `go run .`, then also build an executable with `go build`
main.go
package main

import "fmt" // TODO: turn this into a grouped import that adds runtime

func main() {
    // TODO: print a "Setup report" heading
    // TODO: print the Go version (a function call)
    // TODO: print GOOS and GOARCH joined with "/" (constants)
    // TODO: print the number of CPU cores (a function call)
}

Expected: Varies by machine, for example: Setup report Go version: go1.24.4 OS/Arch: windows/amd64 CPU cores: 8

Show hint

`runtime.GOOS` and `runtime.GOARCH` are string constants, so you can join them with `+ "/" +`. `Version()` and `NumCPU()` are functions, so call them with parentheses.

Show solution
main.go
package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println("Setup report")
    fmt.Println("Go version:", runtime.Version())
    fmt.Println("OS/Arch:", runtime.GOOS+"/"+runtime.GOARCH)
    fmt.Println("CPU cores:", runtime.NumCPU())
}

Quick quiz 1 / 3

Right after installing Go, go version in an already-open terminal says 'go' is not recognized. What is the most likely fix?

Quick quiz 2 / 3

The module path is example.com/hello. On Windows, what file does go build (no arguments) produce?

Quick quiz 3 / 3

main.go and helpers.go are both package main, and main.go calls a function from helpers.go. What does go run main.go do?

What to remember

  • Install the toolchain from go.dev/dl and confirm it with go version in a new terminal.
  • Start every project with go mod init; go.mod records the module path and Go version.
  • go run . runs the whole package in the folder; go build produces an executable.
  • Setup errors usually come from PATH, the wrong folder or a missing go.mod.

Go release history — Go

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Get Started (Setup) | Thuta Learning