Your First Go Program
Writing, running, and building a minimal Go program, and understanding the pieces that make it work.
2 min read
With Go installed, it's time to write something and actually run it. Go programs are refreshingly simple to get started with — no build configuration files, no bundler setup, just a .go file and the go command.
Writing the program
Create a new folder, initialize a module, and add a file called main.go:
mkdir hello-go && cd hello-go
go mod init hello-go// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
name := "world"
fmt.Printf("Hello, %s!\n", name)
}Breaking it down
package main — every Go file starts with a package declaration. main is special: it tells the compiler this package produces an executable, not a reusable library.
import "fmt" — brings in the standard library's formatting package. Go's standard library is large and genuinely good, so a lot of what you'd reach for a third-party package for in other languages is already built in.
func main() — the entry point. When you run the program, execution starts here. There's no ambiguity about where a Go program begins, unlike languages where "the entry point" depends on how the file is invoked.
fmt.Println vs fmt.Printf — Println prints its arguments with a trailing newline, no formatting needed. Printf takes a format string with placeholders (%s for strings, %d for integers, %v for "whatever this is, print something sensible") and substitutes in the arguments that follow, similar to printf in C.
Running it
go run main.gogo run compiles the program to a temporary binary, executes it immediately, and cleans up afterward. It's the fastest way to iterate while developing — you don't manage the binary at all.
Building an actual binary
When you want a standalone executable you can ship or deploy:
go buildThis produces a binary named after your module (or you can name it explicitly with go build -o hello). That file is self-contained — it doesn't need Go installed to run, doesn't need any runtime, and can be copied straight onto a server or into a minimal Docker image. This is a large part of why Go is popular for deployment: docker build a Go service and the resulting image can be tiny, often just the binary and nothing else.
Formatting your code
Go ships an opinionated formatter, gofmt, and the community convention is to never argue about style — you just run it:
go fmt ./...This rewrites indentation, spacing, and alignment to Go's canonical style across every file in your project (./... means "this directory and everything under it"). Most editors run it automatically on save. There's real value in this: because everyone's Go code is formatted identically, diffs stay focused on actual logic changes instead of whitespace bikeshedding.
From here, the next lessons dig into Go's actual syntax — variables, types, and the building blocks you'll use in every program you write.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.