Go basics

The structure of a Go program, printing with fmt, variables, types and constants, and reading input.

About 4 hours of study

Learning objectives

Lessons

  1. 1

    Your first Go program

    How a Go program is laid out, what package main and func main mean, how go build turns source into a program, and how to print with fmt.Println, fmt.Print and fmt.Printf.

  2. 2

    Variables, types and constants

    Declaring variables with var and :=, the basic types int, float64, string, bool, byte and rune, zero values, converting between types, and constants.

  3. 3

    Reading input

    Reading numbers and words with fmt.Scan, reading whole lines with bufio.Scanner, splitting lines with strings.Fields and converting text to numbers with strconv.Atoi.

Module practice

Module quiz

1. Which package name must a runnable Go program use?
2. What does the short form x := 5 do?
3. Which Printf verb prints a string?
4. Why does fmt.Scan(&n) need the & sign?
5. What is 7 / 2 when both numbers are ints?
6. Which type is safest for a value that may pass 3 billion on this site?

Control flow and functions ›