Your first Go program
What you'll learn:
- explain what parts a Go program consists of and where its execution starts;
- read a compiler message and fix the error it found;
- import packages from the standard library and call their functions;
- write your own function that takes numbers and returns a result.

A program is like a recipe. A recipe lists its steps in order, and the cook follows them one after another, from top to bottom. A computer runs a program the same way: step by step. But a cook can guess what the author meant, and a computer cannot. It does exactly what is written.
There is one more difference. A Go program is text, but a processor runs only machine code. So the program's text is first translated into machine code, and the compiler does that.
Picture a strict proofreader who reads the recipe before it reaches the kitchen. If the text breaks even one rule of the language, the recipe never gets to the kitchen.
Here is a program that prints one line:
package main
import "fmt"
func main() {
fmt.Println("Hello, world") // Hello, world
}Let's go through it line by line:
package mainsays that the file belongs to an executable program, not to a library.import "fmt"brings in the packagefmt—ready-made code from the Go standard library that prints text. A package is like a jar from the pantry: you do not cook it, you take it off the shelf.func main() {opens the functionmain.fmt.Println("Hello, world")is the recipe's only step: printHello, world.}closes the function.
Where execution starts
Execution starts in the function main. A function is a named block of code, and its body is the statements in curly braces after func main().
The body of main is the recipe the computer follows:
- the statements run one after another, from top to bottom;
- after the last one, the program ends.
Printing with fmt.Println
The notation fmt.Println reads as the function Println from the package fmt. It works by three rules:
- it prints what is inside its parentheses and then moves to a new line;
- it prints text in double quotes as it is, without the quotes;
- if the parentheses hold several values separated by commas, it puts a space between them.
Here are both cases—one value and two:
fmt.Println("first") // first
fmt.Println("second", "line") // second line- The first line prints
firstand moves to a new line. - The second line has two values in its parentheses, so a space appears between the words, and the output has no quotes.
Building and running
To run a program, take three steps:
- Save the program's text in a file with the
.goextension, such ashello.go. - Type the command
go run hello.goin a window for text commands. - The command passes the file to the compiler and runs the result right away.
If the text breaks the language rules, the compiler names the error and its line, and the program does not run.
Here the proofreader picture stops working. A real proofreader would catch a misspelled word, but the compiler checks the rules of the language, not what the author meant. It lets a typo in "Helo, world" through: you see it only in what the program prints.
Comments
Text from // to the end of a line is a comment, and the compiler skips it. In the course examples, a comment at the end of a line shows what that line prints.
Common mistake
The opening curly brace is moved to the next line:
package main
import "fmt"
func main()
{ // ✗
fmt.Println("Hello, world")
}syntax error: unexpected semicolon or newline before {A semicolon ends a statement, and Go code omits it: the compiler adds it at the end of a line. Let's see what that leads to here:
- The compiler adds a semicolon at the end of the line
func main(). - The function ends there and is left without a body.
- The brace
{on the next line is now out of place, and the build stops.
That is why the opening brace in Go always stays on the same line as func main().
Key points
- The compiler translates a program's text into machine code and does not run the program if a language rule is broken.
- Execution starts in the function
mainof the packagemain, and its statements run from top to bottom. fmt.Printlnprints values separated by spaces and moves to a new line.- The opening curly brace stays on the same line as
func main().