1.

What are Go Interfaces?

Answer»

Go interfaces are those that have a defined set of method signatures. It is a custom type who can take values that has these methods implementation.  The interfaces are abstract which is why we cannot create its instance. But we can create a variable of type interface and that variable can then be assigned to a concrete value that has methods required by the interface. Due to these reasons, an interface can act as two things:

  • Collection of method signatures
  • Custom types

They are created by USING the type keyword followed by the name needed for the interface and finally followed by the keyword interface. The syntax goes as follows:

type name_of_interface interface{// Method signatures}

Consider an example of creating an interface of the name “golangInterfaceDemo” having two methods demo_func1() and demo_func2(). The interface will be defined as:

// Create an interfacetype golangInterfaceDemo interface{ // Methods demo_func1() int demo_func2() float64}

Interface also promotes abstraction. In Golang, we can use interfaces for creating common abstractions which can be used by multiple types by defining method declarations that are compatible with the interface. Conside the following example:

package main import "fmt" // "Triangle" data typetype Triangle struct { base, height float32} // "Square" data typetype Square struct { length float32} // "Rectangle" data typetype Rectangle struct { length, breadth float32} // To calculate area of trianglefunc (triangle Triangle) Area() float32 { return 0.5 * triangle.base * triangle.height} // To calculate area of squarefunc (square Square) Area() float32 { return square.length * square.length} // To calculate area of rectanglefunc (rect Rectangle) Area() float32 { return rect.length * rect.breadth} // Area interface for achieving abstractiontype Area interface { Area() float32} func main() { // Declare and assign values to varaibles triangleObject := Triangle{base: 20, height: 10} squareobject := Square{length: 25} rectObject := Rectangle{length: 15, breadth: 20} // Define a variable of type interface var shapeObject Area // Assign to "Triangle" type variable to the Area interface shapeObject = triangleObject fmt.Println("Triangle Area = ", shapeObject.Area()) // Assign to "Square" type variable to the Area interface shapeObject = squareobject fmt.Println("Square Area = ", shapeObject.Area()) // Assign to "Rectangle" type variable to the Area interface shapeObject = rectObject fmt.Println("Rectangle Area = ", shapeObject.Area())}

In the above example, we have created 3 types for the shapes triangle, square and rectangle. We have also defined 3 Area() functions that calculate the area of the shapes based on the input object type PASSED. We have also defined an interface NAMED Area and we have defined the method signature Area() within it. In the main function, we are creating the objects, assigning each object to the interface and calculating the area by calling the method declared in the interface. Here, we need not know specifically about the function that needs to be called. The interface method will take care of this considering the object type. This is called abstraction. The output of the above code will be:

Triangle Area = 100Square Area = 625Rectangle Area = 300


Discussion

No Comment Found