AppDividend
Latest Code Tutorials

Go Interface Tutorial With Example | Interface Implementation in Go

0

Go Interface Tutorial With Example | Interface Implementation in Go is today’s topic.

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. A Go interface is a type that consists of the collection of method signatures.

Go Interface Tutorial With 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 which can be automatically implemented by the types already written.

In Go we’ve two concepts related to interfaces:

  1. Interface : a set of methods required to implement such an interface. It’s defined using keyword interface.
  2. 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.

In the above code, we have defined one interface called DanceOn.
Now that interface has declared one method called Dance().
We have defined the two Go structs.
1) BreakDance
2) AerialDance
Both the structs implements the DanceOn interface that is why they both have to implement the Dance() method.

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.

Go Interface Tutorial With Example

 

Interface makes the code more scalable, flexible, and it’s a path to achieve the polymorphism in Go programming language. Instead of requiring the particular type, interfaces allow specifying that only some behavior is needed. A set of methods defines behavior.

 

Why use Go Interfaces

Go interfaces are a way to construct a 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 or data objects (structs whose members are basic types or other data objects).

Conclusively, Go Interface Tutorial With Example | Interface Implementation in Go is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.