Interfaces for External Dependencies

Introduction

In any software application, it’s common to interact with external dependencies, like databases, message queues, and APIs. To ensure that our code is testable, maintainable, and flexible, it’s important to design interfaces that abstract these dependencies away from the rest of the code. In this lesson, we’ll discuss how to design interfaces and write methods for dealing with external dependencies in Go.

Designing interfaces for external dependencies

When designing interfaces for external dependencies, we want to make sure that they are easy to use, maintain, and test. Let’s go through some key pointers that need to be kept in mind.

Use interfaces to abstract external dependencies

The first step is to define an interface that abstracts the external dependency. This allows us to write code that interacts with the interface without having to worry about the implementation details of the external dependency.

For example, let’s say we have a database that we want to interact with. We can define an interface that provides methods for interacting with the database.

Get hands-on with 1200+ tech skills courses.