Sunday, 27 Nov, 2022
HomeTechnologyGolang Interfaces- A Brief Tutorial

Golang Interfaces- A Brief Tutorial

- - 0

Interface is a highly powerful and essential part of development since Golang is a statically typed language. In other words, development in Golang is brought closer to the OOPs model by the interfaces. We will learn everything there is to know about interfaces in this tutorial, along with examples of how to utilize them in programmes.

So let’s get going.

What is Meant by an Interface?

We are able to achieve polymorphism thanks to interfaces. It enables us to specify the behavior of the object by representing a collection of method signatures. To put it simply, keep in mind that the interface has certain methods. When a type provides definitions for each of those methods, it is said to implement interfaces.

Simply said, an interface is a way to see a collection of related types or objects. You should be aware that you cannot create an instance of the interface since it is abstract. If you grab the concept now, you will have an advantage during Golang development

The best way to grab the concept is to code. So, let us get to it then. 

Analyzing a Program 

We’ll start by making a directory called “interface.” I’ll be saving all of my projects in go-workspace, so I’ll type:

cd go-workspace 

mkdir interface 

cd interface

code .

When we enter code, the IDE or text editor launches. In my case, I have Visual Studio Code. When the text editor opens, we create a Go source file, named main.go. 

package main 

import (

“fmt”

)

type trapezium struct {

basex float64

basey float64

height float64

}

type rhombus struct {

diagx float64

diagy float64

}

func (t trapezium) area() float64 {

return ((t.basex+t.basey)/2)*t.height

}

func (r rhombus) area() float64 {

return ((r.diagx*r.diagy)/2) 

}

  • The above-mentioned section of code is simple to understand. Here, we are importing the package ‘fmt’. Following this, we are creating two structs. These are rhombus and trapezium shapes.
  • The trapezium struct’s three data fields, basex, basey, and height, are all of type float64. The diagx and diagy data fields of the rhombus struct are both of the float64 data type.
  • The behavior of the trapezium and the rhombus is thus analogous; they have a “area” in common. You may still talk about the trapezium and rhombus areas even though they each have different implementations.
  • It is now obvious that the trapezium and rhombus both have forms and areas. Due to the interface, the types (rhombus and trapezium) appear to be interchangeable. The interface does not take a look at the specifics. 

Meanwhile, if you stumble upon an issue with backend development in Golang, you must not hesitate to get in touch with professional experts at Golang.Company. They will assist you in developing a scalable, robust and fast application. 

Defining the Interface 

After the import(), we will type: 

type form interface {

area() float64

}

The form interface has been built as a result. Any type or struct that has the area() method and returns a float64 is considered to be of the form type by this interface. It effectively implements the interface form, in other words. Now, the interface may be utilized as a struct’s upper level type.

Also Read: Key Considerations When Hiring Custom Mobile App Development

Utilizing the Interface 

Now, we will type in the function main. 

func main() {

r1 := rhombus {6,4}

t1 := trapezium {8, 6, 4}

forms := []form {r1, t1}

}

The interface is used in the same way as any other. The rhombus and trapezium may be plugged into the slice using the type form as they both implement the form interface. Once you use it as a type, the remaining real properties of the rhombus and trapezium are unreachable through the interface. Only the area method is accessible.

Incorporating a For Loop 

for _, form := range forms {

  fmt.Println (form.area())

Once we run the program, we get to see the following output:

run programme Golang

You are free to utilize all of the defined behavior and the objects in the type interface if you implement the interface.

Incorporating a Function 

  • One of the best qualities of interfaces as types is their adaptability. They can be used as return types, parameter types, or variable types. The slice is not always necessary.
  • You are free to include them on a map anyway you choose. Now that all the different kinds are available within the interface, there is more flexibility. Now let’s write a function.

func Result (f form) float64 {

return f.area()

}

  • The Result function, which we are creating, accepts a f form parameter and returns a float64 result. Additionally, it will return f.area (). The shape we are supplying here might be a rhombus or a trapezium as long as it has the area method connected.
  • Then, we return to the main function. We’ll also come back to the fmt.Println statement.

fmt.Println (Result (form))

(Point to be Noted: This is a different application of the program, so we are coding it separately in Go source file named tutorial.go) 

What if the Methods Accept Pointers? 

In this section, we will make the methods accept pointers 

func (t *trapezium) area() float64 {

return ((t.basex+t.basey)/2)*t.height

}

func (r *rhombus) area() float64 {

return ((r.diagonalx*r.diagonaly)/2) 

As a result, the main function will also require some changes. We cannot use r1 and t1 in the statement forms:= []form {r1, t1} because they are values rather than pointers. The answer to the problems is in the next statement.

forms := []form {&r1, &t1}

In fact, while utilizing slices, it is a good practice to pass the pointer. This is because you may use the pointer if necessary because it is available to you.

Now, if we run the program, you will see the result as follows:

Golang tutorial

By now, you should have a good grasp of the concept of interface. If you are unable to comprehend the tutorial, it is a must that you go through the instructions once again. Also, taking a look at other examples would help a lot. The key is to practice more, so that you have no issues implementing it in intricate projects to simplify your workload.

No Comments

Search

Categories

Trending Post

copyright©2019 DigitalDrona. All rights Reserved.