Skip to content

The Go Compiler

Go is a compiled language. This means your source code is converted into machine code (binary) before it is executed.

Two Ways to Run Go

Command Action Best for...
go run Compiles to a temp folder and runs immediately. Development and testing.
go build Compiles to a permanent executable binary. Production and shipping apps.

Building for Production

When you run go build, Go creates a self-contained binary. This binary includes everything it needs to run—no need to install Go on the server!

1
2
3
4
5
# Creates a file named 'hello' (or 'hello.exe' on Windows)
go build hello.go

# Run the binary
./hello

Cross-Compilation

One of Go's best features is the ability to build binaries for other operating systems from your current machine.

1
2
3
4
5
# Build for Windows (from Mac/Linux)
GOOS=windows GOARCH=amd64 go build hello.go

# Build for Linux (from Windows/Mac)
GOOS=linux GOARCH=amd64 go build hello.go

Why is the Go Compiler special?

  1. Fast: Even large projects compile in seconds.
  2. Static Binaries: By default, Go binaries don't depend on external libraries (like libc).
  3. Optimization: The compiler automatically removes unused code (dead code elimination) to keep binaries small and fast.