liveBook · Manning

Chapter 3: Primitive types and operators

What you will learn in chapter 3 of Go in Action, Second Edition

The chapter introduces Go’s built-in types as fundamental building blocks and previews the types and operations it will cover.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces Go’s built-in types as fundamental building blocks and previews the types and operations it will cover.

Key terms: primitive types, built-in types, statically typed, numeric operations, bool, structs

3.1 Integer types

This section introduces Go’s integer types, their sizes and aliases, and how to declare and write integer values.

Key terms: int, uint, signed, unsigned, two’s complement, int64, uint8, architecture-independent, architecture-dependent, byte

3.1.1 Choosing an integer type

This section explains how to choose Go integer types, why unsigned integers can cause surprising results, and when unsigned types are useful.

Key terms: int, int64, int32, int16, int8, len(), unsigned integers, wrap around, uint, two’s complement

3.2 Floating-point number types

This section introduces Go’s floating-point types, their precision tradeoff, declaration defaults, and ways to write floating-point literals.

Key terms: floating-point, IEEE-754, float32, float64, precision, var, short declaration, zero value, floating-point literal, decimal point

3.2.1 Special floating-point values

This section explains how Go represents exceptional floating-point results and how to check for them.

Key terms: IEEE-754, math.IsInf, math.IsNaN, overflow

3.2.2 Choosing a floating-point type

This section recommends float64 as the default floating-point type and explains when float32 may be justified.

Key terms: float64, float32, precision, performance, memory consumption, bottleneck

3.3 Complex number types

This section introduces Go’s native complex number types, how to create them, and how to extract their real and imaginary components.

Key terms: complex64, complex128, real part, imaginary part, complex(), imaginary literal, real(), imag(), float32, float64

3.4 Mathematical operators

This section explains which Go numeric types support arithmetic and bitwise operators, the type rules for arithmetic, and how bit clear works.

Key terms: binary arithmetic operators, remainder, modulo, literals, bitwise operations, bit clear operator, &^, bitwise OR

3.5 The bool type

Introduces Go’s bool type, its values and zero value, and the type inferred for Boolean expressions.

Key terms: bool, true, false, zero value, Boolean expression, short variable declaration

3.5.1 Boolean functions

This section shows how Boolean-returning helper functions make conditions readable and keep more complex decision logic in one place.

Key terms: bool, Boolean helper function, conditional logic, IsEven, IsWorkHours

3.6 Struct types

This section introduces structs as bundles of named fields, shows how to access and initialize them, and explains why reusable struct types are preferable to repeated one-off definitions.

Key terms: struct, fields, struct keyword, dot notation, struct literal, short declaration, trailing comma, one-off structs, struct types

3.6.1 Structs as types

The section shows how naming a struct type makes it reusable and simplifies variable declarations.

Key terms: structure type, person, user-defined types, field

3.7 Pointer types

Introduces pointers as typed references to memory locations and shows how to declare a pointer, recognize its nil zero value, and obtain an address with &.

Key terms: pointer, memory address, map, slice, *T, nil, address operator, &, points to

3.7.1 Dereferencing pointers

This section explains how to distinguish a pointer’s address from the value it points to and how to access or modify that value.

Key terms: pointer, address, dereferencing, *variableName

3.7.2 Creating pointers with new and literals

This subsection compares ways to create pointers directly and explains when to prefer a struct literal or new.

Key terms: address operator, literal, new, struct fields, built-in types

3.8 The string type

Introduces Go’s string type as read-only bytes for text and explains why Unicode and byte interpretation matter.

Key terms: Unicode, string, read-only array of bytes

3.8.1 Interpreted strings

This subsection introduces double-quoted interpreted string literals and the characters and escape codes they can contain.

Key terms: interpreted string, UTF-8, escape codes, Unicode, codepoint

3.8.2 Raw string values

This subsection introduces Go raw strings and how their contents are interpreted.

Key terms: raw string, backtick characters, escape codes, raw Unicode characters

3.8.3 String operations

This subsection introduces string comparison by byte values and concatenation with the + operator.

Key terms: comparison operators, byte values, concatenated, +

3.8.4 len() and Unicode characters

This section explains why Go’s len function and byte-by-byte loops behave differently for ASCII and multibyte Unicode strings.

Key terms: len, bytes, UTF-8, ASCII, backing array, fmt.Printf, %x, traditional for loop, Unicode

3.8.5 Iterating Unicode strings using a range loop

This subsection explains how a range loop iterates over a Unicode string one character at a time.

Key terms: range loop, string, index, rune value, bytes

3.8.6 The rune type

Explains how runes represent Unicode characters independently of their string encoding and points to packages for inspecting them.

Key terms: rune, int32, Unicode character, encoding, unicode, unicode/utf8

3.8.7 Converting a string to a slice of runes

This section shows how converting a Go string to a slice of runes lets you index its characters by position.

Key terms: rune, []rune, slice of runes, character position

3.9 Summary

The chapter summary recaps how Go’s primitive types support everyday values, conditional logic, grouped data, references, and text.

Where this is used later

Concepts explained in this chapter

← Chapter 2: Diving into Go Chapter 4: Collection types →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.