Monday, October 5, 2015

Go Interfaces for Absolute Beginners

The concept of Interfaces and its practical implementation may be rather daunting for beginners. It may require immense time and patience to assimilate the idea of interfaces.

Interfaces are an abstraction that helps you to achieve the following objectives with your code:

                  1. Clean & readable.
                  2. Scalable.

An interface defines a set of methods without any implementation code. So, an interface is simply an abstraction and can't contain any variables or code.


Declaration format of interfaces:

type Logger interface {
            method1 (param list) return type
            method1 (param list) return type
}

As per the official recommendation:
  • In the above format, Logger is the name of interface. It is idiomatic in Go to suffix the name of interface with er in most of the cases. There are exceptions but a common practice for naming an interface is: 
        • method name + er
  • Interfaces with one or two methods are common in Go code. More than three methods in an Interface is not considered idiomatic.
  • A type can implement multiple interfaces. Interface can be implemented by any type. But an interface can't implement itself.
  • Multiple types can implement the same interface.
  • Like many other languages, in Go also, we must implement all the methods in the interface.


Here's a code snippet (Code Ref# 1) that finds the area of a rectangle and triangle using functions. The same exercise is refactored in Code Ref#2 using structsmethods and interfaces.

Code Ref# 1. Play Here


package main

import "fmt"

func AreaRectangle(l, w int) int {

 return l * w
}

func AreaTriangle(b, h int) int {

 return (b * h) / 2 //Area of a triangle is (base*height)/2
}

func main() {

 fmt.Println("Area of Rectangle:", AreaRectangle(3, 4))
 fmt.Println("Area of Triangle:", AreaTriangle(5, 20))

}

Output

Area of Rectangle: 12
Area of Triangle: 50

Code Ref# 2. Play Here

package main

import "fmt"

type Rectangle struct {
 width, height int
}

type Triangle struct {
 base, height int
}
type Shaper interface {
 Area() int
}
// You must implement all the methods in the interface
func (s Rectangle) Area() int {
 return s.width * s.height
}
func (t Triangle) Area() int {
 return (t.base * t.height) / 2
}
// Variable declared as an interface type can call methods that are in the named interface.
// Generic 'measure' function that works on any Shape.

func measure(s Shaper) {
 fmt.Println(s)
 fmt.Println(s.Area())
}
func main() {
 s := Rectangle{width: 3, height: 4}
 t := Triangle{base: 5, height: 20}
// The Rectangle struct type implements the 'Shaper' interface so we can use instances 
// of these structs as arguments to measure.
 measure(s)
 measure(t)
}



Output



{3 4}
12
{5 20}
50
P.S. Interfaces are a bit difficult to understand. A beginner must persist with it, practice as many examples as possible to understand and assimilate the concept. I'll review this post every now and then and may add a few more code samples.


No comments:

Post a Comment