Golang Interface Example | Interface Implementation in Go
Golang interface is a type defined using a set of method signatures. It describes the behavior of a similar kind of objects. A Go interface is a type that consists of the collection of method signatures.
If you are new to Go language then, you may be heard or seen in the code about interface{} a lot in code snippets and tutorials. This can be very daunting seeing this at first. Interface implementation is one of the core concepts of Object-oriented programming language.
If you have used Java, PHP, or Python then you might know what interface is and how it can be useful to build a scalable application.
An interface is a type defined using a set of method signatures. It describes the behavior of a similar kind of objects. Go programming provides another data type called interfaces which represents a set of method signatures.
The struct data type implements these interfaces to have method definitions for the method signature of the interfaces.
Golang Interface example
The interface is defined using the type keyword, followed by a name and the keyword interface. Then we specify a set of method signatures inside curly braces.
A prominent feature of Golang is that interfaces are implemented implicitly. The programmer doesn’t have to specify that type T implements interface I. That work is done by the Go compiler (never send a human to do a machine’s job).
The nice implication of these behaviors is the possibility to define an interface that can be automatically implemented by the types already written.
In Go we’ve two concepts related to interfaces:
- Interface : a set of methods required to implement such an interface. It’s defined using keyword interface.
- Interface type: variable of interface type which can hold any value implementing a particular interface.
Syntax of Go Interface
type interface_name interface { method_name1 [return_type] method_name2 [return_type] method_name3 [return_type] }
By defining an interface in Golang, we essentially define a contract which in the future must be implemented. If we define a type based on the interface, then we are forced to implement all of the methods or functions defined within that interface type. An interface contains a method name and its return type. Go structs implement methods. It is like the class in other programming languages.
Implementing an interface in Go
Go interfaces can be implemented as methods on the structs. In many programming languages like Python, Java, or PHP, there is a concept of the base class that can be used to implement the shared functionality used by all sub-classes. Go (rightfully) prefers composition to inheritance.
Let’s get an example; let say we wanted to define an interface for a DanceOn. We could define our interface to include a Dance() function like the following.
type DanceOn interface { // Dance prints out "Let's dance" to the terminal Dance() }
With our DanceOn interface defined, we could define the BreakDancer and AerialDancer structs which implements the DanceOn interface.
See the below code example.
// hello.go package main import "fmt" // DanceOn Interface Defined type DanceOn interface { Dance() } // BreakDance Struct Defined type BreakDance struct { Name string } // AerialDance Struct Defined type AerialDance struct { Name string } // Dance Function implemented from DanceOn Interface func (b BreakDance) Dance() { fmt.Printf("%s is doing Breakdance on the floor\n", b.Name) } // Dance Function implemented from DanceOn Interface func (a AerialDance) Dance() { fmt.Printf("%s is doing Aerialdance on the air\n", a.Name) } func main() { var breakdancer BreakDance breakdancer.Name = "Michael Jackson" breakdancer.Dance() var aerialdancer AerialDance aerialdancer.Name = "Krunal" aerialdancer.Dance() }
If we want to implement an interface, you need to implement all the methods declared in the interface. Go Interfaces are implemented implicitly
Unlike other languages like Java, you don’t need to explicitly specify that a type implements an interface using something like an implement keyword.
Then inside the main() function, we have declared the struct variable breakdancer and aerialdancer.
Then we have assigned the Name property to value Michael Jackson and Krunal.
Finally, called the Dance() function and printed the message inside the console of the terminal. See the below output.
Why use Go Interfaces
Go interfaces are a way to construct the backbone of your program.
Objects should interact with each other through the interfaces and not through the concrete objects. It means that you should build an object model for your application that consists only of the interfaces and basic types of data objects (structs whose members are basic types or other data objects).
Conclusively, Go Interface example | Interface Implementation in Go is over.
In the example code I can remove the interface part and i will still do the exact same thing,
so the fundamental question “what does an interface DO?” is still unanswered,
you define what it is, how it compares to other languages, but not why should we
use it in our code.
You’re right. He’s not utilizing the interfaces he created.
func main() {
var dancer DanceOn = BreakDance{“Michael Jackson”}
dancer.Dance()
dancer = AerialDance{“Krunal”}
dancer.Dance()
}
If you’re to extend the interface to include another method named “Sing()”, then both struct created initially have to implement those methods too so they can conform to the interface update.