Setting Up a Go Project
Introduction
Before writing any Go code, you need to create a module — Go's name for a project. Every Go project has a go.mod file that declares the module name and the version of Go it requires. This is roughly equivalent to a Python pyproject.toml, but simpler.
Unlike Python, where you can create a .py file and run it immediately, Go always requires this module setup first. This is part of Go's design philosophy: a little more structure upfront leads to fewer surprises in large codebases.
Go is a compiled language. Your source code is translated into a native binary by the Go compiler before it runs. This means:
- The compiler catches many errors (type mismatches, unused variables) before your program ever executes.
- The resulting binary runs without needing Go installed on the target machine.
- Go programs are typically much faster at runtime than equivalent Python programs.
GitHub Copilot tip: Throughout these exercises, use Copilot Chat to ask questions about any command or concept you don't understand. Try asking: "What does go mod init do and why is it needed?"
Concepts
The Go module system
- Every Go project lives inside a module, declared by a
go.modfile. - You create a module with
go mod init <module-name>. - The module name is usually a URL path (e.g.
github.com/yourname/myproject), but for local exercises a simple name likeexercisesis fine.
Key Go CLI commands
go mod init <name>— initialise a new modulego run <file>.go— compile and run a file in one stepgo build— compile to a binary executablego fmt <file>.go— auto-format your code (like Black for Python)go vet— report likely mistakes in your code
Project structure
A minimal Go project needs only two things: a go.mod file and at least one .go source file containing a main function.
Examples
# Create a project folder and initialise the module
mkdir go-exercises
cd go-exercises
go mod init exercises
The resulting go.mod file:
module exercises
go 1.22
# Run a Go file directly (compiles and executes in one step)
go run main.go
# Compile to a binary called 'myapp' (or 'myapp.exe' on Windows)
go build -o myapp main.go
./myapp
# Format all Go files in the current directory
go fmt ./...
Challenges
1. Create your module
- Open a terminal in VS Code.
- Create a folder called
go-exercisesand navigate into it. - Run
go mod init exercises. - Open the
go.modfile and read its contents.
Ask Copilot: What does the go directive in go.mod control?
💡 Show Answer
mkdir go-exercises
cd go-exercises
go mod init exercises
The go directive (e.g. go 1.22) tells the Go toolchain the minimum version of Go this module requires. It also enables or disables certain language features that changed between versions.
2. Verify your installation
- Run
go versionand note the output. - Run
go env GOPATHandgo env GOROOT.
Ask Copilot: What is the difference between GOPATH and GOROOT?
💡 Show Answer
go version # e.g. go version go1.22.0 windows/amd64
go env GOPATH # where your downloaded packages are cached
go env GOROOT # where the Go toolchain itself is installed
GOROOT is where Go is installed. GOPATH is your workspace — where downloaded modules and compiled binaries go. You rarely need to set these manually with modern Go.
3. Compare to Python setup
- In Python you might use
python -m venv envandpip installto set up a project. - Write down (or ask Copilot to explain) the equivalent steps in Go.
Ask Copilot: How does Go handle third-party dependencies compared to Python's pip?
💡 Show Answer
In Go, third-party packages are added with go get <package-path>. Go records them in go.mod and downloads them to a local cache. There is no separate virtual environment — the module system handles isolation per project. Running go mod tidy removes unused dependencies, similar to pruning a requirements.txt.
4. The go fmt command
- Create a file called
main.gowith some deliberately badly-indented code (copy any snippet and remove spacing). - Run
go fmt main.go. - Observe what changed.
Ask Copilot: Why does Go have an official formatter, and what problem does it solve?
💡 Show Answer
Go ships with gofmt, an opinionated formatter with no configuration options. Every Go file in the world is formatted the same way. This eliminates arguments about style in code reviews and makes all Go code immediately readable to any Go developer — regardless of who wrote it. Python has black and autopep8, but these are optional third-party tools, not part of the language itself.
5. go run vs go build
- Create a
main.gofile containing a simplefmt.Println("Hello")program. - Run it with
go run main.go. - Then compile it with
go build -o hello main.goand run the resulting binary. - Explain in your own words the difference between the two approaches.
💡 Show Answer
go run compiles and immediately executes the code, without creating a file on disk. It is the fastest way to test code during development — equivalent to python main.py.
go build compiles the code into a standalone binary (hello or hello.exe). This binary can be copied to any compatible machine and run without Go installed. There is no Python equivalent — Python always requires an interpreter present.
6. Go vs Python: when to choose which?
Ask Copilot: In what situations would you choose Go over Python, and when would you stick with Python?
Read the answer and note at least three reasons for each. There are no right or wrong answers here — this is about building intuition.
💡 Show Answer
Choose Go when:
- You need high performance and low latency (e.g. network servers, APIs handling thousands of requests per second)
- You are building a tool that needs to be distributed as a single binary with no dependencies
- You need true parallelism (goroutines vs Python's GIL)
- You want the compiler to catch type errors before deployment
Choose Python when:
- You are doing data science, machine learning, or scientific computing (NumPy, pandas, PyTorch)
- You need rapid prototyping with minimal boilerplate
- You are scripting or automating tasks
- Your team is more familiar with Python and development speed matters more than runtime speed