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 versionYou should see output similar to:
go version go1.22.x linux/amd64If 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 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-goThe 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.goEither way, you should see:
Hello, GoUnder 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.exeChoose 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.goIf you want to see the formatted output without changing the file, omit -w:
gofmt main.goWhat “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 helpGet help for a specific command:
go help run
go help build
go help modWhen 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 envCheck specific values (useful for debugging):
go env GOPATH
go env GOMOD
go env GOROOT
go env GOOS
go env GOARCHGOMODshows the path to the activego.modfile (or an empty value if you are not in a module).GOPATHis still used for things like the module download cache and installed tools, even when you use modules.GOOSandGOARCHindicate the target OS/architecture for builds.
Exercise: create, run, format, and build “Hello, Go”
- Create a new folder named
hello-goand initialize a module withgo mod init. - Create
main.gowith apackage mainand afunc main()that printsHello, 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 helpfor the command you’re using andgo env GOMODto confirm you’re inside the module.