Golang Program to Create an Interface


In this article, we are going to learn about how to create an interface using golang programming

INTERFACE − In the go language, an interface is a custom type that is used to specify one or more method signatures. An interface is abstract i.e. we cannot create an instance of the interface but we can create a variable to the interface type and assign this variable to the struct or class that has methods needed by the interface.

Syntax

type interface_name interface {
   // method signatures
}

Example 1

To define an interface first we need to use the type keyword it is an indication that we are defining a new type we need to decide the name of the interface followed by the keyword interface then we need to mention the method signatures inside this interface.

package main

import "fmt"
// package fmt allows us to print anything on the screen

// creating a structure named temp and assigning it a property of count
type temp struct {
   count int
}

// creating an interface named inter
type inter interface {
   // defining method signatures in the interface
   getCount() int
}

// defining and creating a function to the structure that returns an integer value
func (t temp) getCount() int {
   // returning the value of the count
   return t.count
}

func main() {
   // creating a variable to the interface type
   var struct_intr inter

   // creating a new instance to the struct and assigning value to it
   st := temp{98}

   // Assigning the interface variable to the struct so that we could use the functions defined in
   //  the interface
   struct_intr = st

   // accessing the getCount() method from the instance to the struct
   fmt.Println("Accessing the getCount() method from the st instance \ncount=", st.getCount(), "\n")

   // priting the count by calling the getCount() method from the struct_inter interface variable
   fmt.Println("Accessing the getCount() method from the interface variable\ncount=", struct_intr.getCount(), "\n")
}

Output

Accessing the getCount() method from the st instance
count= 98 

Accessing the getCount() method from the interface variable
count= 98 

Description

  • Import the fmt package that allows us to print anything on the screen.

  • Create a temp structure with count as a key in it.

  • Create an interface named inter and define method signatures in it.

  • Defining the getCount() function to the structure that returns the integer value as the count.

  • Now we need to define a variable to the interface type.

  • Assign this variable to the structure defined above this will let us use the methods defined in the interface from the struct’s instance.

  • Create an instance of the structure and provide an integer number as the value to the count key.

  • Now, we can access the getCount() method both from the interface variable as well as the struct’s instance.

  • Print the value of the count by calling the getCount() method first from the interface and then from the struct variable.

Example 2

Using Type Assertion to get a value of a concrete type back and can call methods on it that are defined on another interface, but aren't part of the interface satisfying.

package main
// defining the package’s main
// fmt package allows us to print anything on the screen
import "fmt"

// defining an interface named Polygons and defining method signatures in it
type Polygons interface {
   // defining a method signature named Perimeter()
   Perimeter()
}

// defining an interface named Object and defining a method in it too
type Object interface {
   NumberOfSide()
}

// creating a struct named Pentagon that stores integer values.
type Pentagon int

// defining a method named Perimeter() to the int struct
func (p Pentagon) Perimeter() {
   // printing the perimeter on the screen
   fmt.Println("Perimeter of Pentagon", 5*p)
}

// defining a method named NumberOfSides() to the int struct
func (p Pentagon) NumberOfSide() {
   // getting the number of sides of the pentagon
   fmt.Println("A pentagon has 5 sides")
}

func main() {
   // Giving the integer value to the pentagon struct
   // further assigning the struct to the interface so that we could use the methods defined in it
   var p Polygons = Pentagon(50)
   // using the perimeter method to print the perimeter of the pentagon on the screen
   p.Perimeter()

   // assigning the instance to the struct so that we could use the methods present in it.
   var o Pentagon = p.(Pentagon)
   // using the NumberOfSides() method to calculate the number of sides of the pentagon
   o.NumberOfSide()

   // Giving the integer value to the pentagon struct
   // further assigning the struct to the interface so that we could use the methods defined in it
   var obj Object = Pentagon(50)

   // using the NumberOfSide() method to print the perimeter of the pentagon on the screen
   obj.NumberOfSide()

   // printing the perimeter from the obj method
   var pent Pentagon = obj.(Pentagon)
   pent.Perimeter()
}

Output

Perimeter of Pentagon 250
A pentagon has 5 sides
A pentagon has 5 sides
Perimeter of Pentagon 250

Description

  • Import the fmt package that allows us to print anything on the screen.

  • Create a new structure named Pentagon that accepts integer values.

  • Create two interfaces named Polygon and object and define method signatures in them.

  • Defining the perimeter() and numberOfSides() function to the structure that will print the perimeter and number of sides of the pentagon respectively.

  • Giving the integer value to the pentagon struct and assigning the struct to the interface so that we could use the methods defined in it.

  • Note we are using the numberOfSIdes() that is defined in the obj interface, not in Polygon.

  • Again, assign the pentagon int to the Pentagon struct and assign it to the obj interface.

  • Again, we are using the methods defined in the Polygon interface from the instance created over the obj interface.

  • In this way we can call methods that are defined on other interfaces but aren't part of the interface satisfying.

Updated on: 22-Dec-2022

997 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements