Go Foundations in Go

Go Foundations is where you start writing Go.

In this chapter, you will:

  • learn what Go is, why Google made it, and where it is used
  • set up Go and run your first program
  • store values in variables and constants
  • learn Go's main data types, and how to convert between them
  • do maths and comparisons with operators

What each lesson covers:

  • Introduction to Go: What Go is, why Google made it, its history, features and philosophy, where it is used, how to set it up with Go modules, and how to write your first program with comments.
  • Variables and Constants: How to declare, initialize and change variables with var and :=, make constants with const and iota, name things, and ignore values with the blank identifier.
  • Data Types: Go's number types, strings, booleans, bytes and runes, zero values, nil, type inference, and converting between types with T(x) and strconv.
  • Operators: Arithmetic, increment and decrement, relational, logical, assignment and bitwise operators in Go, and the order in which Go works out an expression.

This chapter has 4 lessons. Plan on about 90 minutes, with 15 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and you can write small Go programs that remember, calculate and print values.

Lessons in this section

  1. Introduction to Go

    What Go is, why Google made it, its history, features and philosophy, where it is used, how to set it up with Go modules, and how to write your first program with comments.

    • Explain what Go is and why Google created it
    • Name the main features and the main uses of Go
    • Install Go, check the version, and create a project with go mod init
    • Write a program with package main, import, main() and statements
  2. Variables and Constants

    How to declare, initialize and change variables with var and :=, make constants with const and iota, name things, and ignore values with the blank identifier.

    • Declare, initialize, assign and reassign variables
    • Choose between var and := and declare many variables at once
    • Make typed and untyped constants and number them with iota
    • Follow Go's naming rules and know its keywords
  3. Data Types

    Go's number types, strings, booleans, bytes and runes, zero values, nil, type inference, and converting between types with T(x) and strconv.

    • Choose the right integer, float or complex type
    • Use string, bool, byte and rune values
    • Predict the zero value of any type and explain nil
    • Let Go infer types with := and write types when needed
  4. Operators

    Arithmetic, increment and decrement, relational, logical, assignment and bitwise operators in Go, and the order in which Go works out an expression.

    • Do maths with + - * / % and avoid integer-division surprises
    • Use ++ and -- the Go way
    • Compare values and combine answers with && || !
    • Shorten updates with += and friends