Reader small image

You're reading from  Protocol Buffers Handbook

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781805124672
Edition1st Edition
Right arrow
Author (1)
Clément Jean
Clément Jean
author image
Clément Jean

Clément Jean is the CTO of Education for Ethiopia, a start-up focusing on educating K-12 students in Ethiopia. On top of that, he is also an online instructor (on Udemy, Linux Foundation, and others) teaching people about diff erent kinds of technologies. In both his occupations, he deals with technologies such as Protobuf and gRPC and how to apply them to real-life use cases. His overall goal is to empower people through education and technology.
Read more about Clément Jean

Right arrow

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 {
  ...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Protocol Buffers Handbook
Published in: Apr 2024Publisher: PacktISBN-13: 9781805124672

Author (1)

author image
Clément Jean

Clément Jean is the CTO of Education for Ethiopia, a start-up focusing on educating K-12 students in Ethiopia. On top of that, he is also an online instructor (on Udemy, Linux Foundation, and others) teaching people about diff erent kinds of technologies. In both his occupations, he deals with technologies such as Protobuf and gRPC and how to apply them to real-life use cases. His overall goal is to empower people through education and technology.
Read more about Clément Jean