Golang Tutorial — Methods and Interfaces
If you want to apply some design patterns (read more: https://sourcemaking.com/design_patterns) in Go program, such as Adepter, Proxy, or Decorator, or even in testing you Go program, you might need to use methods and interfaces in you code. So this article will explain to you about what they are and how to use them. If you’re ready to learn, let’s go through it!
What is a method?
A method is a function or a piece of code that is associated with an object.
Let’s say, you have a struct called “Car” and you create a method of the struct named “Drive”, then you declare an object from the struct and name it as “tesla”. So you can call the “Drive” method of your “tesla” to do whatever you programmed the method to do by using the properties or fields of your “tesla” to go the calculation.
What I just explained to you is only an example to make you get the concept of method. So now I will show you how to create your own methods and how to use them.
Usage of Method
Basically, all methods are associated with objects, means that you need to have at least one object in your code to declare any methods.
After you got your struct, you will be able to create your method
A method is very similar to a function but the difference is that the method is associated with an object and can you the properties inside the object to calculate, just like the “Drive” method that uses “distance” property of “Car” to calculate new “distance”.
In case you need to expose some fields of your struct to other packages without directly exporting them, using methods as “Getter” is your best choice.
Setter is a way to set the value of unexported fields and getter can be used to setup default value of any field you want like the example below.
Now you have already learnt how methods work and how to work with methods, but there is another feature in Golang that can enhance the power of structs and their methods. The feature is “Interface”.
What is an interface?
An interface is a set of method signature that an object can implement.
To make it more understandable, let’s say you have another struct named “Plane” and its methods: “Fly” and “GetDistance”. And you want to use it with “Car” struct by using the same logic to get their distance. An interface will help you for this kind of task.
From the example above, you can use the same function with different parameter types by using interface as argument. And before you use the method from the interface, you need to do “type assertion” which is very important step for using interfaces.
Anyway the code can be improved and shorten by declaring an interface type with a set of method signature. For our case, it is “GetDistance” method. You will see how short it is.
However, don’t forget to validate your interface before using it because somehow it can be nil.
I hope you would get some point about implementing interface with multiple types, it will allow you to create only one logic for executing their method and make you life easier 😂.