Adding entries
In this section, let’s focus on adding a Person
or a Company
contact to the AddressBook
. We will start with the business logic (pkg
) and then we will link the business logic to the CLI part of our application (cmd
).
The business logic
We will first add a new file under pkg/addressbook
called add.go
. This will contain all the code related to the addition of contact. In this file, we will have two functions: AddPerson
and AddCompany
. These functions are similar; however, they receive different information as parameters. Let’s first talk about the similarities between these two functions.
The first similarity is that both functions take db
as a parameter. Once again, we will use an interface to keep this code as generic as possible. This time, however, we will use the io.ReadWriter
interface since we need both Read
and Write
functions. So, we have the following (pkg/addressbook/add.go
):
func AddPerson(db io.ReadWriter, ...) error { ...