Purpose of Interface in GO

Bibek Magar
wesionaryTEAM
Published in
3 min readOct 6, 2020

--

If you are from JavaScript, JAVA or any other programming language background it will took time to understand the purpose of interface in GO.
Interface in GO are quite different from other languages.

Interfaces in GO
Interface are the custom type that is used to specify a set of one or more method signatures which are allowed to create a variable of an interface type and this variable can be assigned with a concrete type value that has the methods the interface requires.
It is just kind of tool which make code clearer, cleaner,shorter and provide a nice API between packages, or clients(users) and servers(providers).

Example without using interface:


package main
import (
"fmt"
)
type apple struct{}
type ball struct{}
func main() {
a := apple{}
b := ball{}
showRoundApple(a)
showRoundBall(b)
}
func showRoundApple(a apple) {
fmt.Println(a.getRoundMessage())
}
func showRoundBall(b ball) {
fmt.Println(b.getRoundMessage())
}
func (apple) getRoundMessage() string {
return "Hello! I am apple and I am round"
}
func (ball) getRoundMessage() string {
return "Hello! I am ball and I am round"
}
-----------------------------------------------------------------Output:
bee@bee-desktop:~/Coding/Practice/GO/Interfaces$ go run main.go
Hello! I am apple and I am round
Hello! I am ball and I am round

As we can see showRoundApple and showRoundBall has similar nature that is just printing the return message but the difference is only in arguments(parameters). So, to implement functions for both same nature we use interface.

The idea behind go interfaces is duck typing. Which simply translates into: If you look like a duck and quack like a duck then you are a duck. Meaning that if your object implements all duck’s features then there should be no problem using it as a duck.

Example with using interface:

package mainimport (
"fmt"
)
type round interface {
getRoundMessage() string
}
type apple struct{}
type ball struct{}
func main() {
a := apple{}
b := ball{}
showRound(a)
showRound(b)
}
func showRound(r round) {
fmt.Println(r.getRoundMessage())
}
func (apple) getRoundMessage() string {
return "Hello! I am apple and I am round"
}
func (ball) getRoundMessage() string {
return "Hello! I am ball and I am round"
}
-----------------------------------------------------------------Output:
bee@bee-desktop:~/Coding/Practice/GO/Interfaces$ go run main.go
Hello! I am apple and I am round
Hello! I am ball and I am round

As we can see, we create a new interface type round which share a common property between apple and ball.
We specify that round is an interface which have getRoundMessage function and return string. So, now we can execute showRound function which will be applicable for both apple and ball type. This is very helpful when we want different types to behave in the same manner.

A practical example would be file type objects (sockets, file objects) — you need a Write and a Read function on all of them. Then you can use Write and Read in the same fashion independent of their type — which is cool.

Conclusion

As conclusion usage of interface helps code looks cleaner,clearer, shorter and readable which also act as bridge between users and the providers.

References:
- https://stackoverflow.com/questions/7042640/usage-of-interface-in-go
- https://www.udemy.com/course/go-the-complete-developers-guide/

Thank you for reading :)
Follow me on @beevekmgrz

--

--