Why Go Interfaces Are the Remarkable Secret to Clean Code Mastery
- With Code Example
- August 29, 2024
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.
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 -
- What are interfaces in Go?
- Working example with explanation
- Why go interfaces are useful in Golang
- 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
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