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.
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.
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.
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() }
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 '{}'.
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") }
Now, we can see the Structure 'student' has both the Functions, 'bags()' and 'onTime()'.
And the Interface is binded with the Structure 'student'.
Now, it is time for us to create an 'Interface' i.e. 'schoolRules' type variable.
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.
Then we call the 'bags()' Function.
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'.
Similarly, the other Function 'onTime()' is called printing the values.
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.
And binded it with the Structure.