Categories
Golang

Golang: Interface

In Go or Golang, declaring an interface is pretty simple and easy.

We just defined an interface named Printer that required an implementer to have a method named Print which takes a string parameter and returns nothing. Interfaces are implemented implicitly in Go. Any type that has the Print(string) method implements the interface. There is no need to […]

In Go or Golang, declaring an interface is pretty simple and easy.

We just defined an interface named Printer that required an implementer to have a method named Print which takes a string parameter and returns nothing. Interfaces are implemented implicitly in Go. Any type that has the Print(string) method implements the interface. There is no need to use any implements keyword or anything of that sort.

In the above example, the Terminal type implements the Printer interface because it implements the methods required by the interface. Here’s a runnable, full code example:

We declared our printer variable to be of type Printer which is the interface. Since the Terminal type implements the Printer interface, we can pass Terminal{} to the printer variable and later call the Print method on it.

Interface and the Method sets

As you can understand, a method set is a set of methods on a type. The method set of an interface type (for example Printer here) is it’s interface, that is the Print method in this example. The method set of a type T (for example Terminal) contains the methods which can take a T type receiver. In our above code, the Print method takes the type Terminal so it’s included in Terminal‘s method set. The corresponding pointer type, *T has a method set that includes all methods with a receiver type of *T as well as the methods defined on the receiver type T. So *Terminal type contains any method that either takes Terminal or Terminal as a receiver type. So the Print method is also in the method set for *Terminal .

Method Set of T includes all methods receiving just T.
Method Set of *T includes all methods receiving either T or *T.

So the method set of *T includes the method set of T anyway. But by now, you might be wondering why this is so important. It is very important to understand the method set of a type because whether it implements an interface or not depends on the method set. To understand things further, let’s take a quick look at the following example:

If you try to run this code on the go playground or try to run/compile it on your machine, you shall get an error message like this:

Can you guess what’s happening? Well, our Print method has a receiver type of *Terminal however we are trying to assign the type Terminal to printer. The Print method falls in the method set of *Terminal and not Terminal. So in this particular example, *Terminal type actually implements the interface, not the base Terminal type. We can just assign &Terminal to printer and it will work fine. Try the codes here – https://play.golang.org/p/MvyD0Ls8xb 🙂

Another interesting thing, since *Terminal also includes the method set defined on Terminal, this could would be valid just fine – https://play.golang.org/p/xDmNGBcwsM. This is why understanding the method set of a type is important to understand which interfaces it implements.

The Curious Case of Method Calls

We have seen how the method set of *T includes methods receiving both T and *T but the method set of T is confined to methods that only take T and not *T. Now you might be thinking – I have seen codes like the following snippet:

Here, the Print method receives a *Terminal type but how are we calling it on Terminal type? From what we have seen before, Terminal should not have the method set defined to take a *Terminal receiver, how is this call being made?

Well, the code x.m() works fine if the m method takes the x type as receiver. That is fine with us. But if the method m is to take the type *x and we try to call x.m() – that shouldn’t work, right? The proper call should be (&x).m() – no? Yes, correct. But Go provides us a shortcut here. If the method m is defined to take a *x type as receiver and base type x is addressable, x.m() works as a shortcut for (&x).m(). Go provides us with that shortcut to keep things simpler. So whether you have a pointer or a value, it doesn’t matter, as long as the type is addressable, you can call the method set of *x on x using the very same syntax. However, please remember that this shortcut is not available while working with interfaces.

The Empty Interface

The type interface{} has zero methods defined on it. And every type in Go implements zero or more methods. So their method set actually satisfies the emtpy interface aka interface{}. So if a variable is of type interface{}, we can pass any type to it.

We want to store different types in the same slice? Map values can be of different types? Just use interface{}.

So, when we’re not sure of a type, or we need the type to flexible / dynamic, we can use interface{} to store them.

Type Assertion

While we can store any type in a interface{} type, not all types are the same. For example, you can not use the string functions on an integer type. Go would not accept if you blindly want to pass interface{} in an operation where a very specific type is expected. Take a look:

Even though we have a string value stored against the name key, Go actually stores it as a type interface{} and thus it won’t allow us to use it like a string. Luckily, interface values do store the underlying value and type. So we can use type assertion to assert that the underlying value can behave like a certain type.

This works:

The unKnownMap["name"].(string) part – we’re doing the type assertion here. If the type assertion succeeds, we can use the value as a string. If it does not succeed, we will get a panic.

Getting Type of an interface{}

If you have an interface{} and want to know what it holds underneath, you can use the %T format in Printf family of calls.

Type Switch

You can also use a switch statement with an interface{} to deal with different possible types.

The i.(type) gets you the type of the variable. Please remember it only works with a switch statement.