Basic

Your First Go Program

Print text to the screen and understand the minimal structure of a Go program.

What you will see

Hello, Go!

About 2 min of reading

Try It

You will write your very first Go program, watch the tiny structure that a runnable Go file must have, and see exactly how fmt prints to the screen.

1

I start with the smallest possible runnable program: a package that imports the formatting library and calls fmt.Println one time. I run it and read the single line of output.

package main import "fmt" func main() { fmt.Println("Hello, World!") }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, World!
A Go program that you run must live in a directory that is part of a module. If the command complains, run `go mod init hello` in the same folder first.
2

I add a second call to see the difference between the two fmt functions in the package. fmt.Println adds a newline after its text; fmt.Print does not.

package main import "fmt" func main() { fmt.Print("Hi, ") fmt.Println("there!") }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hi, there!
fmt.Print leaves the cursor at the end of its output, so without a Println after it the shell prompt would sit on the same line as your text.
3

I write two fmt.Println calls to confirm a program runs its statements top to bottom, one line at a time.

package main import "fmt" func main() { fmt.Println("first") fmt.Println("second") }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
first second
4

I give the program some real content: a string name and an age, then print them together. Go fills each name into the text exactly where it appears in the list of arguments.

package main import "fmt" func main() { name := "Ada" age := 30 fmt.Println(name, "is", age) }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada is 30
The := operator both declares the variable and picks its type from the value on the right, so name is a string and age is an int automatically.
5

I add both styles of comment around the code. Comments start with // for a single line or / / for several lines. The compiler ignores them, so the output is unchanged from before.

package main import "fmt" // Print a greeting with fmt.Println. func main() { /* a multiline comment the compiler skips */ fmt.Println("Hello, Go!") }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, Go!
Comments cannot be nested, so a */ inside /* */ ends the block early. Keep them short and place them before the line they describe.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

The main function runs top to bottom; fmt.Println sends a formatted string to the terminal.
1 go run compiles main.go into a temporary binary
↓
2 The OS loads the binary and calls main()
↓
3 fmt.Println formats the string with a trailing newline
↓
4 The formatted text is written to standard output
Why this works
The main function

Every executable Go program has exactly one func main(). When the program starts, Go calls main and runs its statements top to bottom.

func main() {
    fmt.Println("first")
    fmt.Println("second")
}

Output:

first
second

Statements end with a semicolon in the grammar, but Go inserts them automatically at line breaks, so you rarely write any.

The standard library

Go ships with a large standard library. fmt handles formatted input and output. Others you will use soon:

  • os: operating system interactions
  • io: reading and writing data
  • math: math functions

Import them with import "package" and call their exported functions like fmt.Println.

Examples

Hello World with fmt.Println

Printing to the screen

Go prints text with the fmt package. Every runnable program starts in func main().

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

package main marks a runnable program. import "fmt" brings in the formatting package. main() is called automatically when the program starts.

Why it works: the fmt.Println function writes its arguments followed by a newline to standard output.

Running a program

Building and running

Save the code above as main.go, then run:

go run main.go
# or
go build -o hello main.go && ./hello

go run compiles and executes in one step. go build produces a standalone executable binary.

Watch out: Go files must live in a directory that is part of a module. Run go mod init hello first to create one.

Comments and structure

Annotating code

Go has two comment styles:

// single-line comment

/* multi-line
   comment */

Comments are ignored by the compiler but help humans read the code. Use // inline and / / for longer notes.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Variables and Constants
Variables and Constants →