Dev Skills
Go·Section 1 of 15 · 8 lessons · about 6 h 30 min

Golang basics: setup, syntax, types and control flow

Install it, run something, and meet the ideas that make Go different from what you already know.

  1. 01Setup, your first program, and the toolchain
    Install Go, run one file, and meet the four commands you will use every day.
  2. 02Syntax, comments, and naming
    The shape every Go file takes, and the naming rules the whole ecosystem follows.
  3. 03Printing in Go: Println, Printf and format verbs
    How to print in Go with Print, Println and Printf, and the Printf format verbs you will use every day.
  4. 04Basic data types
    Every built-in type, why there are so many integer sizes, and what to actually use.
  5. 05Constants and iota
    Values fixed at compile time, and how Go does enums without an enum keyword.
  6. 06Operators
    Arithmetic, comparison, logical, and bitwise, plus what Go leaves out on purpose.
    Graded challenge
  7. 07Type conversion
    Why Go refuses to turn an int into a float for you.
  8. 08if, for, and switch
    One loop keyword, no ternary, and cases that do not fall through.
    Graded challenge

What each lesson covers

By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.

  1. 1. Setup, your first program, and the toolchain

    • Write and run a package main program with go run ..
    • Explain what the zero value of an int, a string and a bool is.
    • Predict what go build, go fmt and go vet each do before you run them.

    Remember: Go compiles to one self-contained binary with nothing to install beside it, and every variable starts at a defined zero value. Those two facts shape most of the language.

  2. 2. Syntax, comments, and naming

    • Write a Go file with a package clause, an import block, and a doc-commented function.
    • Explain why the opening brace must stay on the same line as func main().
    • Fix the compile errors caused by an unused import and an unused variable.

    Remember: Capitalisation is not style in Go. It is the access modifier.

  3. 3. Printing in Go: Println, Printf and format verbs

    • Choose between Print, Println, Printf and Sprintf for a given output.
    • Predict the output of Printf with %v, %T, %q, %.2f and %5d.
    • Write a literal percent sign and a padded column with format verbs.

    Remember: %v prints anything and %T tells you its type. Learn them before you learn a debugger.

  4. 4. Basic data types

    • Choose the right built-in type for a count, an id, a decimal and a money amount.
    • Predict the output of overflowing an int8 or a uint8.
    • Explain why 0.1 + 0.2 is not exactly 0.3 for float64 variables.

    Remember: Go gives you exact-size types for when it matters, and int for when it does not.

  5. 5. Constants and iota

    • Write a const block that numbers a set of states with iota.
    • Explain why an untyped constant fits a float64 and an int64 but a typed one does not.
    • Predict the values iota produces in a block with a skipped first line and arithmetic.

    Remember: There are no constant slices, maps or structs. Those are var.

  6. 6. Operators

    • Predict the output of integer division, %, and shifts such as 3 << 10.
    • Write a boolean expression with %, && and || that answers a yes/no question.
    • Explain why d != 0 && 10/d > 2 never divides by zero.

    Remember: None of Go’s operators can be redefined. a + b means addition, or concatenation for strings, in every file you will ever read.

  7. 7. Type conversion

    • Write explicit conversions between int, float64 and a named type.
    • Explain why string(rune(65)) gives "A" while strconv.Itoa(65) gives "65".
    • Predict the output of len on a string with non-English characters.

    Remember: Implicit numeric conversion is where money code loses cents. Go makes you write it down.

  8. 8. if, for, and switch

    • Write the same loop as a three-part for, a condition-only for and a range loop.
    • Explain why a variable declared in if n := ...; cond cannot be used after the block.
    • Predict which switch case runs, knowing that cases do not fall through.

    Remember: There is no ternary operator. Write the if.

Test yourself