Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - INTERFACES


Say you are a newly recruited Principal of a reputed School.


And you see that the students students are quite naughty.


So, you have decided that there should be two behaviours that a Student needs to follow,while in school.

  1. bags() - Every student should come with school bags.
  2. onTime() - Every student should come to school on time.

So, we have seen that we already have a structure for Student.


type student struct {  
    name string  
    roll int  
    age int  
} 

Also we have seen in the earlier tutorial that the Students have a behaviour called 'speak()'.


For simplicity let us not include the 'speak()' behaviour in this tutorial.


Now, the question is, how would you implement the behaviours, 'bags()' and 'onTime()' for every Students?


Well! The answer is 'Interface'.


All you need to do is, create an Interface with the two behaviours and bind it with the 'student' Structure.


java_Collections

Just remember, the Interface won't define the Functions(i.e. 'bags()' and 'onTime()'). But the Functions(i.e. 'bags()' and 'onTime()') would be defined and binded to the Structure.


Let us simplify with the below example.


Example :



package main
import "fmt"
    
type schoolRules interface {
    
    bags()
    onTime()
}
    
type student struct {
      
    name string  
    roll int  
    age int  
} 
    
func (s student) bags() {
    fmt.Println("My name is",s.name,"and I have my bag")
}
    
func (s student) onTime() {
    fmt.Println("My name is",s.name,"and I have arrived school on time")
}
    
     
func main() { 
     
    var schRul1 schoolRules
    schRul1 = student{name: "John", roll: 65, age: 10 }  
        
    schRul1.bags()
        
    schRul1.onTime()
        
    var schRul2 schoolRules
    schRul2 = student{name: "Rakhi", roll: 50, age: 8 }
        
    schRul2.bags()
        
    schRul2.onTime()
    
} 


Output :



 My name is John and I have my bag
 My name is John and I have arrived school on time
 My name is Rakhi and I have my bag
 My name is Rakhi and I have arrived school on time

So, in the above example, we have declared the interface that contains the rules imposed by the Principal(i.e. You).


type schoolRules interface {
    bags()
    onTime()
} 

So, defining an interface is quite simple, it begins with a 'type' keyword followed by the interface name followed by the 'interface' keyword. And the functions should be inside the braces '{}'.


java_Collections

java_Collections

Now, to use this Interface, the Structure 'student',


type student struct {
    name string  
    roll int  
    age int  
}

Must define both the functions, 'bags()' and 'onTime()' and the functions should be tied to the Structure.


And as we can see, both the functions, 'bags()' and 'onTime()' are defined and tied to the Structure.


func (s student) bags() {
    fmt.Println("My name is",s.name,"and I have my bag")
}
    
func (s student) onTime() {
    fmt.Println("My name is",s.name,"and I have arrived school on time")
}

java_Collections

Now, we can see the Structure 'student' has both the Functions, 'bags()' and 'onTime()'.


java_Collections

And the Interface is binded with the Structure 'student'.


java_Collections

Now, it is time for us to create an 'Interface' i.e. 'schoolRules' type variable.


var schRul1 schoolRules

And we have the 'Interface', 'schoolRules' variable named 'schRul1'.


Now since, the 'Interface', 'schoolRules' is binded with 'student' Structure.


We can use the 'Interface' variable 'schRul1' to initialise and access the Structure type element.


schRul1 = student{name: "John", roll: 65, age: 10 }

java_Collections

java_Collections

Note : schRul1 is an Interface type variable and not Structure type variable.

Then we call the 'bags()' Function.


schRul1.bags()

And the 'bags()'Function gets called.


func (s student) bags() {
    fmt.Println("My name is",s.name,"and I have my bag")
}

Printing the value of 'schRul1'.


My name is John and I have my bag

Similarly, the other Function 'onTime()' is called printing the values.


Moral of the Story


So, the moral of the story is that you being a Principal have imposed the rules of bags() and onTime() that every student must obey because you have declared an Interface type variable.


var schRul1 schoolRules

And binded it with the Structure.


schRul1 = student{name: "John", roll: 65, age: 10 }