Topic Overview and Goals

Welcome to our deep dive into one of the powerful facets of Go programming - interfaces. Think of these as a job description that defines a role's required skills, just as interfaces specify the functions that our Go types need to implement. By the end of this lesson, you will be able to confidently create effective interfaces in Go.

Understanding What Interfaces Are

An interface, in computing, is a communication point between objects; it defines behaviors that an object can implement. Consider a soccer player who must kick a ball, sprint, and follow game rules — these behaviors are akin to methods in an interface.

Internally, an interface in Go is represented by a tuple (type, value), comprising the concrete type of the value it holds and the value itself. This design enables Go's runtime to perform dynamic type checking and allows interfaces to be versatile. When a value is assigned to an interface, Go stores this tuple, facilitating type assertions and interface conversions by preserving the concrete type's information.

One important thing to note is that an interface in Go retains its type even when holding a nil value; thus, it's only truly nil if both the type and value are nil. When not checked properly, this can lead to subtle bugs.

Declaring Interfaces in Go

To declare interfaces in Go, we use the type keyword, followed by the interface's name, the interface keyword, and a set of methods enclosed within {} brackets. Take a look at the example below:

Here, we've created a Player interface with two methods: Play and Score. Any type that implements this interface must define these methods.

How Interfaces Interact with Go Types

А type implements an interface by implementing its methods. In Go, there is no explicit declaration that a type implements an interface. To demonstrate this, let's create a struct, Footballer, that implements the Player interface:

By defining the Play and Score methods for Footballer, it now implements the Player interface.

Understanding Go's Interface Conversions and Type Assertions

A type assertion provides access to an interface value's underlying concrete value. An interface can hold any type of value and sometimes you might want to get the actual type of the value that is being held in the interface. Here's where type assertions become necessary.

t := i.(T) This statement asserts that the interface value i holds the concrete type T and assigns the underlying T value to the variable t. Let's consider an example:

In this example, value := s.(Student) uses a type assertion to extract the struct value from an interface.

Empty Interface and its Usage

An empty interface in Go has no methods. Since all types inherently implement no methods, every type implements the empty interface. Here's how to use it:

The PrintAnything function, which accepts arguments of any type, exemplifies the usage of the empty interface.

The Practical Benefits of Interfaces in Go

Understanding interfaces theoretically is beneficial, but observing their practical applications illustrates their true power in Go programming. Interfaces foster flexibility and decoupling in code design, making it easier to manage and extend. Let's explore this through an example:

Consider an application that sends notifications. The initial requirement is to send these notifications via email, but soon, you'll need to incorporate SMS and push notifications. This is where interfaces shine. You can define a Notifier interface and then implement this interface for different notification types.

First, define the Notifier interface:

Implement the interface for an Email notifier:

Later on, adding an SMS notifier becomes seamlessly easy, without altering the core logic of your notification system:

This design provides tremendous flexibility. You can add as many notifier types as the application requires over time without modifying the existing notification logic. This exemplifies the principle of "programming to interfaces, not implementations," facilitating easy maintenance and scalability in software projects.

Lesson Summary and Getting Ready for Practice

Great work! You've learned about Go's interfaces, how to define them and how types implement them, as well as interface conversions, type assertions, and the usage of the empty interface. Apply your newfound knowledge of the interfaces in Go during our next practice session! Get ready to ace it!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal