Free Ebook cover Go (Golang) Fundamentals: Simple, Fast Programs for Beginners

Go (Golang) Fundamentals: Simple, Fast Programs for Beginners

New course

12 pages

Go Fundamentals: Setting Up the Go Toolchain and Writing Your First Program

Capítulo 1

Estimated reading time: 5 minutes

+ Exercise

1) Install Go and verify your setup

To write and run Go programs, you need the Go toolchain installed on your machine. After installing Go for your operating system, confirm that your terminal can find the go command and that the installation is working.

Verify the installation

Open a terminal (Command Prompt/PowerShell on Windows, Terminal on macOS/Linux) and run:

go version

You should see output similar to:

go version go1.22.x linux/amd64

If you get “command not found” (macOS/Linux) or “is not recognized” (Windows), Go is not on your PATH yet. Fixing PATH is an OS-level step, but the goal is simple: typing go should work from any folder.

GOPATH vs modules (practical view)

Older Go workflows used a workspace rooted at GOPATH (often $HOME/go) and expected code under $GOPATH/src. Modern Go uses modules by default: each project can live anywhere on your disk, and dependencies are tracked in a go.mod file inside the project.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

In practice for beginners: use modules. You can still have a GOPATH on your machine, but you don’t need to place your project inside it. When you initialize a module, Go knows how to resolve dependencies and build your program from that folder.

2) Create a minimal program and run it with go run

A Go executable program starts in a package named main and must define a function named main. The Go toolchain can compile and run it in one step using go run.

Step-by-step: create a new folder and initialize a module

  • Create a folder for your project and enter it.
  • Initialize a module (this creates go.mod).
# macOS/Linux (example path; choose your own location)
mkdir hello-go
cd hello-go
go mod init example.com/hello-go
:: Windows PowerShell (example)
mkdir hello-go
cd hello-go
go mod init example.com/hello-go

The module path can be a real domain you own, but for learning it can be a placeholder like example.com/hello-go. It just needs to look like an import path.

Create main.go

Create a file named main.go in the folder with this code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go")
}

Run it

From the same folder, run:

go run .

go run . tells Go to run the package in the current directory. You can also run a specific file:

go run main.go

Either way, you should see:

Hello, Go

Under the hood, go run compiles your program to a temporary binary and executes it. It’s convenient for quick iteration.

3) Build a binary with go build

When you want a reusable executable (a binary you can run again without recompiling), use go build. This compiles your program for your current OS and CPU architecture.

Build the program

In your project folder, run:

go build .

This produces an executable in the current directory by default.

Output name and OS differences

  • On macOS/Linux, the output will typically be named after the folder (for example, hello-go).
  • On Windows, executables end with .exe (for example, hello-go.exe).

Run the built binary:

# macOS/Linux
./hello-go
:: Windows PowerShell
./hello-go.exe

Choose an explicit output file

You can control the output path/name with -o:

go build -o bin/hello .

If the bin folder doesn’t exist yet, create it first. This is a common pattern to keep build artifacts separate from source code.

4) Format basics with gofmt (and why it matters)

Go uses a standardized formatting style. Instead of debating brace placement or indentation, Go developers rely on gofmt to format code consistently. This makes code easier to read across teams and reduces noise in code reviews.

Format a file

To format main.go in place:

gofmt -w main.go

If you want to see the formatted output without changing the file, omit -w:

gofmt main.go

What “lint” means here

Strictly speaking, gofmt is a formatter, not a linter. But it provides an important “baseline quality check”: if your code isn’t formatted, it stands out immediately. Many editors can run gofmt automatically on save, which is the easiest way to keep formatting consistent.

5) Use go help and go env to understand your configuration

The Go toolchain is self-documented through go help, and it exposes its configuration through go env. These two commands are your first stop when something behaves unexpectedly.

Explore commands with go help

List common commands and topics:

go help

Get help for a specific command:

go help run
go help build
go help mod

When you see flags like -o or patterns like go run ., go help is where you confirm the exact behavior.

Inspect environment settings with go env

Print all Go environment variables that affect the toolchain:

go env

Check specific values (useful for debugging):

go env GOPATH
go env GOMOD
go env GOROOT
go env GOOS
go env GOARCH
  • GOMOD shows the path to the active go.mod file (or an empty value if you are not in a module).
  • GOPATH is still used for things like the module download cache and installed tools, even when you use modules.
  • GOOS and GOARCH indicate the target OS/architecture for builds.

Exercise: create, run, format, and build “Hello, Go”

  • Create a new folder named hello-go and initialize a module with go mod init.
  • Create main.go with a package main and a func main() that prints Hello, Go.
  • Run it using go run ..
  • Format it using gofmt -w main.go.
  • Build it using go build ., then run the produced binary from your terminal.
  • If anything fails, use go help for the command you’re using and go env GOMOD to confirm you’re inside the module.

Now answer the exercise about the content:

Which workflow best matches how modern Go projects should be set up and executed when you want dependencies tracked correctly?

You are right! Congratulations, now go to the next page

You missed! Try again.

Modern Go uses modules by default: projects can live anywhere, dependencies are tracked in go.mod, and go run . runs the package in the current folder.

Next chapter

Go Syntax Essentials: Variables, Types, Control Flow, and Functions

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.