Stage 1 · Code
Getting Started
go run and go build
Use `go run` for quick feedback and `go build` when you want a reusable executable.
Fast Feedback with go run
go run compiles your code into a temporary executable and runs it immediately. It is perfect while learning because you can make a change and see the result with one command.
go run .The generated temporary executable is cleaned up for you. You usually do not see it.
Running go run . tells Go to run the current package. It scales better than naming individual files once your program grows beyond main.go.
Building a Binary
go build compiles your program and leaves the executable on disk. Use it when you want to run the program later, copy it somewhere else, or share it with someone using the same operating system and architecture.
go build .
# macOS/Linux
./hello-world
# Windows PowerShell
.hello-world.exeThe default executable name usually comes from the module or folder name. Windows executables end in .exe.
| Command | Best for | Output left behind |
|---|---|---|
| go run . | Fast development feedback | No visible binary |
| go build . | Creating an executable file | A binary in the current folder |
| go build -o app . | Choosing a specific output name | The file named after -o |
Choosing Output Names
The -o flag lets you choose where the executable goes and what it is called. This is useful for scripts, release folders, and avoiding confusing default names.
go build -o greeter .
./greeter
# Windows PowerShell alternative
go build -o greeter.exe .
.greeter.exeThe dot at the end still means “build the package in this folder.”
Cross-Compiling
Go can often build a program for another operating system or CPU by setting GOOS and GOARCH. This is called cross-compiling. For example, you can create a Linux binary from macOS or Windows for many simple command-line programs.
# macOS/Linux shell
GOOS=linux GOARCH=amd64 go build -o greeter-linux .
# Windows PowerShell
$env:GOOS="linux"; $env:GOARCH="amd64"; go build -o greeter-linux .
Remove-Item Env:GOOS
Remove-Item Env:GOARCHGOOS names the target operating system. GOARCH names the target processor architecture.
A Windows .exe is not the same as a Linux or macOS executable. Source code is portable; compiled binaries are built for a target platform.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.