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
Your first Python program
Python reads almost like English. Write a small converter, see its output, and open the project in /code to keep editing.
Build a program with AI
The vibe-coder path for Go: the idioms you must recognise, the prompt, and the review that catches dropped errors.
Your first C++ program
C++ trades a little ceremony for a lot of speed and control. Write a first program, learn the compile-then-run cycle, and open it in /code.
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.