TutorialsPreview
LearnDeepReferenceCheatsheetBuild with AI
LearnBuildbeginner12 min

Your first Go program

Packages, a function, a loop — and one command to run it.

You will build

A program that prints a greeting and the squares of 1 through 5.

You will learn

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().

project · go-hello Open in Code
main.goGO
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))
}
}
Hello, Go — open it in Code
Concept · go/functions
Catalog

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.

GO
func square(n int) int {
return n * n
}
bash
$go run main.go
Hello, 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

M
Build·beginner·12 min

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.

B
Build·beginner·9 min

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.

B
Build·beginner·8 min

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 in

No pinned answers yet for this tutorial.