Go Interface
By ski11up.com
Go lang is an open-source programming language like C, having pointers and not strongly typed language. And most importantly it is not an Object Oriented Programming(debatable) language though it has likes of defining packages
and interfaces
like Java
.
Let’s explore the Go
interface
in this post.
App
We will write Shape interface and then calculate the area of different geometrical shapes ex. Circle, Rectangle, and Triangle.
Shape interface
will be having an area()
function which will be implementing.
Circle, Rectangle, and Triangle will be struct
and having specific properties to the respective geometrical shape.
Finally, we will be using the polymorphism and assigning Circle, Rectangle, and Triangle to the interface variable and executing the implemented function.
Code
Go
code for printing the area of different shapes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"math"
)
type IShape interface { (1)
area() float64
}
type Circle struct { (2)
radius float64
}
type Rectangle struct { (3)
len float64
width float64
}
type Traingle struct { (4)
base float64
height float64
}
func (c Circle) area() float64 { (5)
return (2 * math.Pi * c.radius)
}
func (r Rectangle) area() float64 { (6)
return (r.len * r.width)
}
func (t Traingle) area() float64 { (7)
return (t.base * t.height) / 2
}
func main() {
var shape IShape (8)
shape = Circle{2.0} (9)
fmt.Printf("Area of circle is %v", shape.area()) (10)
shape = Rectangle{1.0, 2.0}
fmt.Printf("\nArea of rectangle is %v", shape.area())
shape = Traingle{10.0, 5.0}
fmt.Printf("\nArea of trinagle is %v", shape.area())
}
1 | defining interface Shape having area() function |
2 | defining Circle struct |
3 | defining Rectangle struct |
4 | defining Triangle struct |
5 | area() function implemnted with reciver function with a Circle struct |
6 | area() function implemnted with reciver function with a Rectangle struct |
7 | area() function implemnted with reciver function with a Triangle struct |
8 | defining variable of an interface |
9 | instantiating shape interface variable with Circle struct |
10 | calling the implemented area() function on the shape interface variable |
good to prefix 'I' with the interface name |
Execute Go App
Navigate to the project folder and execute the below command.
$ go run print_area_of_shape.go
Notes
The above code is straightforward, we define the interface, implement it using the receiver function and we are using the polymorphism for printing the area of different geometrical shapes.
Exercise
From the above code remove lines[26..28] and execute the code, you will see the error.
# command-line-arguments
./main.go:5:2: imported and not used: "math"
./main.go:38:8: cannot use Circle literal (type Circle) as type IShape in assignment:
Circle does not implement IShape (missing area method)
Clearly, it shows that we have not implemented IShape area()
function.