Tag Archives: interface

Golang interface method – pointer receiver or value receiver

Recently I have been working with Golang interface.

There are lots of confusions on how to use interface in Golang, particularly how to implement the methods in interface. Should I implement pointer receiver, or value receiver? How to mutate a struct that implements an interface? Can I implement both? Continue reading

Programming concepts in real life – Interface

I did not know about the concept of interface until I took the module CS2020 where we use Java to learn data structures and algorithms. In the first few lectures, I was introduced to the idea of interface. I found it extremely intuitive and useful.

What is interface

In simple terms, an interface in programming is set of functions, but only names and descriptions of the functions, not the actual codes. You can write a program to implement(support) these instructions in some ways. Later, you can tell other programs that your program implements this interface and supports these instructions. Then, other programs will be able to directly execute the instructions without knowing how they are implemented. Interfaces can be extended so that the extended interfaces specifies additional functions.

Interface used in programming

One term that people often hear in tech news is API, which just is Application programming interface. With APIs, we can use functions that other applications implements in our own applications. The concept of interface is also important for the communication between hardware and the software. There is a communication layer between hardware and software called instruction set architecture (ISA), which specifies the operations that the processor should support so that the compliers(software) can use them. Then the actual implementation of the functions is done by the producer of the processor.

Interface in everyday life

However, I realised that the concept of interface is not limited to the field of programming. It is actually ubiquitous in our everyday real life as well. Think about the banks, they are essentially different implementations of the same interface – bank, which specifies 2 basic operations for customers: deposit and withdraw. Different banks would then go on and implement these two functions in different ways, then add some more functions that customers can perform using banks. This may lead to the problem of different banks having different functions and they are not compatible with each other. For example, cheques implemented in bank A may not be the same format as bank B. Hence, we still the concept of interface and by extending the interface to support more functions, different banks can communicate with each other.

More examples of interface in everyday life

Other examples of interface? You can just list almost everything.

A car? Yes, it is an interface that specifies functions like move, board, exit and etc.

A TV? Yes, it is an interface that specifies functions like display channel, switch channels, switch on, switch off, adjust volume and etc.

A house? Yes, it is an interface that specifies functions like live in, demolish, get address and etc.

People may think that, “Oh, actually this concept of interface has always been there before the invention of computers. What is the big deal?” That is precisely the big deal that I want to point out. Programming helps people to analyse and formulate things systematically. Although the concept has been always around, it was not formally identified and given a proper name so that it can be studied. Different sectors and fields do have their own regulating bodies but the more general concepts similar to interface are not explicitly there(or not conclusive to encompass all sectors). With programming terminologies, we are able to identify patterns or practices that are common across different fields and sectors and give it a generic name. I believe there are other programming concepts(perhaps object) with this ability, I just need to learn more and find out more.

More of this