Interface in Go Language

Haluan Mohammad Irsad
4 min readDec 15, 2018
Go Language is support interface

Introduction

It’s been a long time, I didn’t touch any programming language other than Java for over 2 years. Currently, I am trying to learn Go again (I have used it in 2015, for a short project).

In Go, you can compile code faster than in Java (if we used the same machine). And I am happy because of this, really can save a lot of my time.

Because I can spend much time my time to think the best approach to writing the code rather than waiting for compiling the source code, suddenly I missed my Java advantage, inheritance.

In Java or most of OOP (Object Oriented Programming) world, keyword interface is widely used. It will be same as like trait in FP (Functional Programming) universe. And Go choose to use the keyword interface.

In Java, you have to explicitly tell the class to implements defined interface in the concrete class, so Java compiler can understand your intention, but in Go it’s done implicitly.

As a Java engineer, I often used the interface, it gives some advantages:

  • create loosely coupled systems
  • more abstraction makes the composition and communication/exchange information between two or more separate components become more flexible
  • hide implementation details of each class from each other
  • reusability of design pattern, I do believe in GoF (Gang of Four) Design Patterns. It can saved my time to simplify complex problems became simple solutions.

Thus, I need to learn how to used interface in Go, and at the first time, it made me a little bit uncomfortable (maybe because the implicit approach). But, after I get-used-to to used it, it makes me happy. Because, I can learn something “old” with the “new” way.

A glimpse of Implicit Implementation

What happened if you faced directly with a shy person? She might be not talking or act as what you heard or what you see because it has “another” meaning beside that. And what you will do, if she is an important person in your life, you will learn more about her behavioural and observing more about it, to get the “real” meanings of her intention.

It is like what you have to do here, to give more attention for Go behaviour and how its works, since the interface implementation is done explicitly.

I’ll give you some example, how we used interface in Go:

Sample code for Inheritance composition in Go

As you can see from the code above:

  1. Starting(e Engine) used to execute Start() from Engine interface, while you run the file, it would print respectively what has been defined in CarEngine and TrainEngine struct. Thus, we can say that both CarEngine and TrainEngine is inside Engine family becauseStarting(e Engine) can execute the defined process of CareEngine and TrainEngine. It is also happened for Stop(e Engine) as well.
  2. If you see in detail, CarEngine and TrainEngine have all that has been defined inside Engine interface. But, in those structs you didn’t find any keywords such as implements or extends or anything that will tell you, that struct is inherited from Engine.
  3. Try to remove(t TrainEngine) Start()and see what you will get. If you get something like this:
cannot use trainEngine (type TrainEngine) as type Engine in argument to Starting:
TrainEngine does not implement Engine (missing Start method)

and

cannot use trainEngine (type TrainEngine) as type Engine in argument to Stoping:
TrainEngine does not implement Engine (missing Start method)

First error, tell us that Starting(e Engine) method can’t receive type TrainEngine for Engine argument because TrainEngine didn’t implement Start() method. The second error mainly warned us the same, but for Stoping(e Engine) method.

Go compiler is helpful here to warn us for inheritance implementation, what causing the error and why the error occurs.

As we can see in the code above, there is no keyword at all to specify inheritance allocation in the struct. Then we can make a new point here, Go will handle the “inheritance” allocation if we write all of the interface’s functions to the struct.

In my humble opinion, it is another side of the beauty of implementing inheritance. Just let see, how long this beauty can help the complex problems become simple solution.

Conclusion

We’ve found that Go inheritance is done by composition. It is very close with most of OOP languages and FP languages, every time you write keyword implements, you should implementing all of the functions/methods that already defined inside interface or trait. It same as like in Go, you should compose the struct to implementing all of the functions of the targeted interface.

Further Discussion

I will write more about Go interface a lot in the future and discuss again with all of you my fellow readers. As I promised before, we will play around with GoF Design Patterns.

Extra Notes: Gopher is Go Language mascot (used as part of the image cover in this article), created by Renee French. Creative Commons Attribution 3.0 licensed.

--

--