Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

360 lines (281 sloc) 10.668 kb

More on interfaces

We discovered the magic of interfaces in the previous chapter, how a simple concept as defining a type's behavior can offer tremendous opportunities.

If you haven't read the previous chapter, you shouldn't read this one, simply because most of what we're about to study depends on a good understanding of the previous chapter.

Shall we begin?

Knowing what is stored in a interface variable

We know that a variable of a given interface can store any value of any type that implements this interface. That's the good part, but what if we wanted to retrieve a value stored in an interface variable and put it in a regular type variable, how do we know what exact type was "wrapped" in that interface variable?

Let's see an example to clarify the actual question:

So, the question confronting us is:

How do we test the type that is stored in an interface variable?

Comma-ok type assertions

Go comes with a handy syntax to know whether it is possible to convert an interface value to a given type value, it's as easy as this: value, ok = element.(T), where value is a variable of type T, ok is a boolean, and element is the interface variable.

If it is possible to convert element to type T, then ok is set to true, and value is set to the result of this conversion. Otherwise, ok is set to false and value is set to the zero value of T.

Let's use this comma-ok type assertion in an example:

Output:

list[0] is an int and its value is 1
list[1] is a string and its value is Hello
list[2] is a Person and its value is (name: Dennis - age: 70 years)

It's that simple!

Notice the syntax we used with our ifs? I hope that you still remember that you can initialize inside an if! Do you??

Yes, I know, the more we test for other types, the more the if/else chain gets harder to read. And this is why they invented the type switch!

The type switch test

Better to show it off with an example, right? Ok, let's rewrite the previous example. Here we Go!

Output:

list[0] is an int and its value is 1
list[1] is a string and its value is Hello
list[2] is a Person and its value is (name: Dennis - age: 70 years)

Now repeat after me:

"The element.(type) construct SHOULD NOT be used outside of a switch statement! -- Can you use it elsewhere? -- NO, YOU CAN NOT!"

If you need to make a single test, use the comma-ok test. Just DON'T use element.(type) outside of a switch statement.

Embedding interfaces

What's really nice with Go is the logic side of its syntax. When we learnt about anonymous fields in structs we found it quite natural, didn't we? Now, by applying the same logic, wouldn't it be nice to be able to embed an interface interface1 within another interface interface2 so that interface2 "inherits" the methods in interface1?

I say "logic", because after all: interfaces are sets of methods, just like structs are sets of fields. And so it is! In Go you can put an interface type into another.

Example: Suppose that you have an indexed collections of elements, and that you want to get the minimum, and the maximum value of this collection without changing the elements order in this collection.

One silly but illustrative way to do this is by using the sort.Interface we saw in the previous chapter. But then again, the function sort.Sort provided by the sort package actually changes the input collection!

We add two methods: Get and Copy:

Output:

The unsorted group is:
(name: Bart - age: 24 years)
(name: Bob - age: 23 years)
(name: Gertrude - age: 104 years)
(name: Paul - age: 44 years)
(name: Sam - age: 34 years)
(name: Jack - age: 54 years)
(name: Martha - age: 74 years)
(name: Leo - age: 4 years)

➞ Younger is (name: Leo - age: 4 years)
➞ Older is (name: Gertrude - age: 104 years)

The original group is still:
(name: Bart - age: 24 years)
(name: Bob - age: 23 years)
(name: Gertrude - age: 104 years)
(name: Paul - age: 44 years)
(name: Sam - age: 34 years)
(name: Jack - age: 54 years)
(name: Martha - age: 74 years)
(name: Leo - age: 4 years)

The example is idiotic (the opposite of idiomatic!) but it did work as desired. Mind you, interface embedding can be very useful, you can find some interface embedding in some Go packages as well. For example, the container/heap package that provides heap operations of data collections that implement this interface:

Another example is the io.ReadWriter interface that is a combination of two interfaces: io.Reader and io.Writer both also defined in the io package:

Types that implement io.ReadWriter can read and write since they implement both Reader and Writer interfaces.

Did I mention yet to make sure you never use element.(type) outside of a switch statement?

Jump to Line
Something went wrong with that request. Please try again.