3 A bookworm’s digest: Playing with loops and maps

 

This chapter covers

  • Ranging over slice and map
  • Using a map to store unique values
  • Learning how to open and read a file
  • Decoding JSON files
  • Sorting a slice with custom comparators

Since the invention of writing, people have been using the tool to carve their thoughts through the centuries. Books were knowledge and became a hobby. We’ve been reading and collecting books on shelves for centuries. With technology, we’re now able to share information more than ever and give our opinion on everything, including books. In this chapter, we’ll join a group of bookworms who have been reading books faithfully. Fadi and Peggy have started registering the books they keep on their bookshelves, and they wonder if we can help them find books that they both have read and, maybe, suggest future reads.

3.1 Loading the JSON data

3.1.1 Defining a JSON example

3.1.2 Opening a file

3.1.3 Parse the JSON

3.1.4 Test it

3.2 Finding books that bookworms have in common

3.2.1 Count the books

3.2.2 Keeping higher occurrences

3.2.3 Determinism

3.3 Print

3.4 Improvements

3.4.1 Implement sort.Interface

3.4.2 Exercise: Reading recommendations

3.4.3 Using bufio to open a file

Summary