Your first Go program
Packages, a function, a loop — and one command to run it.
A program that prints a greeting and the squares of 1 through 5.
package main, func main(), importing fmt, a function, and a for loop.
Prerequisites
Go installed locally to run it, or read along.
Go is deliberately small — few keywords, fast builds, one obvious way to do most things. Every program lives in a package and starts at func main().
package main import "fmt" func square(n int) int { return n * n} func main() { fmt.Println("Hello, Go") for i := 1; i <= 5; i++ { fmt.Printf("%d squared is %d\n", i, square(i)) }}Functions in Go
Parameter and return types come after the name: `func square(n int) int`. The `main` package with a `main` function is what `go run` executes.
func square(n int) int { return n * n}$go run main.goHello, Go
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Milestone
You ran your first Go program.
A package, an import, a function, and a loop — and a single command, `go run`, to build and execute it.
More like this
Make a page respond: a click counter
A page becomes alive the moment JavaScript listens for an event and changes the page in response. Build a counter, then open it in /code.
Build interaction with AI
The vibe-coder path for JS events: the event→state→render loop you must hold in your head, the prompt, and the review.
Build a script with AI
The vibe-coder path for Python: the shape of a small program, the prompt, and the edge cases AI forgets.
Liked this one?
Pass it on
Discussion
Be the first to ask
Your questions stay private with the team. We can pin answers to share with everyone.
Sign in to ask a question privately on this tutorial.
Sign inNo pinned answers yet for this tutorial.