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
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.
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.
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 inNo pinned answers yet for this tutorial.