liveBook · Manning

List method

As explained in Build and Orchestrator in Go (From Scratch)

The store List method returns all tasks in the persistent task store as a slice of task pointers.

List retrieves every task held by a task store. It gives the manager one store-level operation for task collection, replacing direct iteration over an in-memory map. The shared Store interface keeps manager and worker code independent of whether tasks live in memory or persistent storage. section 11.2 section 11.5

The interface signature is List() (interface{}, error), while task-store implementations return a []*task.Task value through that interface result. A caller checks the error, then converts the returned value to the expected task-pointer slice. In GetTasks, the manager returns that slice to expose its current tasks. section 11.2 section 11.5

In-memory collection

An in-memory task store builds the result by declaring an empty []*task.Task slice, ranging over its map, and appending each task pointer. Because this implementation has no failing operation in the loop, it returns the collected slice with a nil error. section 11.3

The same operation has a different implementation cost in persistent storage. A persistent task store opens a read-only BoltDB transaction, iterates through the task bucket, unmarshals each stored JSON value into a task, appends its pointer, and returns any transaction or decoding error. The interface therefore preserves the collection behavior while allowing storage-specific mechanics and failures. section 11.9 section 11.2

Listing 11.6 The List method
func (i *InMemoryTaskStore) List() (interface{}, error) {
   var tasks []*task.Task
   for _, t := range i.Db {
       tasks = append(tasks, t)
   }
   return tasks, nil
}

The minimal in-memory implementation makes the core behavior explicit: range over stored task pointers, append them to a slice, and return the slice. section 11.3

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Works through map iteration and persistent bucket traversal, including JSON decoding and error propagation for the persistent implementation.
Examples
Uses an in-memory task map and a BoltDB task bucket to assemble slices of task pointers.
What it assumes
Assumes familiarity with Go interfaces, slices, maps, pointers, type assertions, and error returns.
What you can do after
Apply it via the Store interface to return all manager tasks without directly accessing the backing map.
How it connects
Connects Store with Put, Get, Count, Manager, Worker, BoltDB, and JSON.

Also explained in

Compare how 3 books explain list method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.