liveBook · Manning

Unsigned integer

As explained in Learn Go with Pocket-Sized Projects

uint is Go's unsigned integer type whose size is implementation-dependent and corresponds to the native word size.

Go's uint is an unsigned integer type whose size follows the implementation's native word size. It suits quantities that cannot be negative, such as the number of copies of a book. section 3.2.1

Counting books

Use uint as the value type in a map counter, then increment the count for each book. A missing map entry starts at uint's zero value, so count[book]++ handles the first occurrence as well as later ones. section 3.2.1

Listing 3.12 bookworm.go: The booksCount() function
// booksCount registers all the books and their occurrences
// from the bookworms shelves.
func booksCount(bookworms []Bookworm) map[Book]uint {
    count := make(map[Book]uint) #1

    for _, bookworm := range bookworms { #2
        for _, book := range bookworm.Books {
            count[book]++ #3
        }
    }

    return count
}

A complete booksCount function combines a map[Book]uint counter with nested range loops. section 3.2.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines uint and demonstrates it in a map counter, without exploring integer representation or conversion rules.
Examples
Uses a booksCount function that records occurrences of books across bookworms and tests expected count maps.
What it assumes
Assumes familiarity with Go maps and loops while reinforcing zero-valued map entries through counting.
What you can do after
Apply via Go map counters to record nonnegative occurrence totals.
How it connects
Connects unsigned integers to Go maps, zero values, range loops, and increment operations.

Also explained in

Compare how 9 books explain unsigned integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.