The Supreme Guide to Golang Interface

When dealing with complex applications, we choose the language which is compatible and can work smoothly with the requirements. There are numerous languages for every purpose you could think of. Similarly, different languages have different implementations of interfaces. In this article, you will go through the implementation of Go Interface.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is an Interface?

The key principle of an interface in Go is to provide method signatures for similar types of objects. Go does not have classes and inheritance to implement object orientation. Go has the interface, an abstract type to fulfill this purpose.

How to Create an Interface?

In Go, you can create an interface using the type keyword, followed by the name of the interface and the keyword interface. And, you can specify method signatures inside curly braces.

Golang_Interface_1.

You will get to understand the creation of interfaces with examples.

Golang_Interface_2

Line no. 1 of the program above creates an interface type named Motorvehicle which has the method Mileage() and Average Speed().

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Implementing an Interface in Go

To implement an interface in Go, you need to implement all the methods declared in the interface. Go Interfaces are implemented implicitly. Unlike Java, you don’t need to explicitly specify using the implements keyword. 

In the above program, we have defined the Motorvehicle interface, which has two methods Mileage and Average Speed that return float64 value. Any type that implements these methods will also implement the Motorvehicle interface.

Golang_Interface_3
Golang_Interface_4

We’ve created the Motorvehicle interface and the struct type BMW in the above program. Then we defined methods like Mileage and AverageSpeed, which belong to the BMW type. Therefore, BMW implemented those methods.

Since these methods are defined by the MotorVehicle interface, the BMW struct type implements the MotorVehicle interface. This shows Go is implicitly implemented.

When a type implements an interface, a variable of that type can also be represented as the type of an interface. This shows an application similar to polymorphism.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Empty Interface

When an interface has zero methods, it is called an empty interface. This is represented by interface{}. Since the empty interface has zero methods, all types implement this interface implicitly.

Golang_Interface_5

Multiple Interfaces

A type can implement multiple interfaces.

Golang_Interface_6.
Golang_Interface_7

In the above program, we created a Motorvehicle interface with the Mileage method and a Newq interface with the AverageSpeed method.Struct type BMW implements both the methods, hence it implements both the interfaces. Hence we can assign a value of struct type BMW to the variable of type Motorvehicle or Newq.

We expect m to have a dynamic value of b and n to also have a dynamic value of b. We used the Mileage method on m of type Motorvehicle interface because it defines Mileage method and AverageSpeed method on n of type Newq interface. After all, it defines the AverageSpeed method.

Type Assertion

You can find the dynamic value of an interface using the syntax i.(Type), where i is a variable of type interface and Type is a type that implements the interface.

Golang_Interface_8.
Golang_Interface_9.

Output:

Golang_Interface_10.

From the above program, the interface variable m of type Motorvehicle has the dynamic value of struct type BMW. We have used m.(BMW) in variable b to extract value.

Now, we can use Mileage and AverageSpeed methods on b since b is a struct of type BMW and BMW implements these methods.

Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!

Conclusion

You have learnt interfaces and saw how they are similar to polymorphism. They are mostly used in the case of functions and methods which accept any value.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

When multiple types implement the same interface, it becomes easy to work with them.

If you aim to build a software development career, you can check the Post Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.

If you have queries or would like to provide your inputs to our editorial team regarding this “The Supreme Guide to Golang Interface” article, feel free to use the comments section below. Our team of SMEs will get back to you soon!

About the Author

Abhisar AhujaAbhisar Ahuja

Abhisar Ahuja is a computer science engineering graduate, he is well versed in multiple coding languages such as C/C++, Java, and Python.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.