Why Go Interfaces Are the Remarkable Secret to Clean Code Mastery

What is Interface, Working Example, Use of Interface, Empty Interface

While researching this article I found that the Go interface is one of the most difficult concepts to learn in golang. This makes me more crazy to write about it and simplify it for you.

1*orY7tDTG3cwsoKSFkZyJbg_328469102911060111.png

Why I Believe Go Interfaces Are the Ignored Powerhouse of Clean Code

I have been using golang for the past 4 years and it is the only language that uses the interface, before that, I used Javascript and PHP for 3 years. While learning I feel the interface concepts are very confusing, So let’s simplify it, in this article, we will learn -

  1. What are interfaces in Go?
  2. Working example with explanation
  3. Why go interfaces are useful in Golang
  4. What is an Empty Interface?

What is Interface in GO

In golang, an interface is a type that is used to define the method signature (definition). It defines and describes the methods that some other types must have. We don’t have class or inheritance in golang to implement the oops concepts, but we can use interfaces for this.

// Define an interface  
type Shape interface {  
    Area() float64  
    Perimeter() float64  
}

Go Interface example

Go Interface example

Go Interface example

In this example I will show you how different shapes can implement the same interface in Go, allowing you to work with polymorphism. read more

Related Posts