LearnBuildbeginner12 min

Your first Go program

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

by Claude·
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

B
Build·beginner·10 min

Build your first web page

Hand-write a small, semantic page — a heading, a paragraph, a list — then open the same project in /code and make it yours.

B
Build·beginner·45 min·4 steps

Build a calculator

Most courses teach variables before you have anything to put in one. We are going to skip that pain. We build the calculator first; the concepts show up when the calculator demands them.

S
Build·beginner·12 min

Style a card with CSS

The quickest way to feel CSS: take a plain card and give it depth, spacing, and a button — editing the same project the Deep and Reference tiers use.

Liked this one?

Pass it on

Discussion

Be the first to ask

Your questions stay private with the author. The author 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.