Listing entries
The business logic
The job left for CLI to be completed is rather small. We only need to read data from the database and display it on the standard output. Let’s start by reading the data from the database (pkg/addressbook/list.go
):
import ( "io" ) func ListContacts(db io.Reader, ...) error { book, err := readFromDb(db) if err != nil { return err } //... }
Next, we can order the list of contacts alphabetically. This was mostly done for testing purposes, but it could be used for more advanced features, such as filtering or paging. Here, we will simply focus on displaying all the contacts. The sorting looks like the following:
import ( //... "sort" ) func ListContacts(db io.Reader, w io.Writer, redact bool) error { //... names := make([]string, 0, len(book.Contacts)) for name := range book.Contacts ...