<\/span><\/h1>\n\n\n\nInterface is a type in Go which is a collection of method signatures. These collections of method signatures are meant to represent certain behaviour. The interface declares only the method set and any type which implements all methods of the interface is of that interface type. <\/p>\n\n\n\n
Interface lets you use duck typing in golang. Now, what is duck typing?<\/p>\n\n\n\n
Duck typing is a way in computer programming which lets you do duck test where we do not check type instead we check the only presence of some attributes or methods. So what really matters is whether an object has certain attributes and methods and not its type.<\/p>\n\n\n\n
Duck typing comes from the below phrase<\/p>\n\n\n\n
If it walks like a duck and quack like a duck then it must be duck<\/code><\/pre>\n\n\n\nComing back to interface again. So what is interface? As mentioned before also it is a collection of method signatures. It defines the exact set of methods that a type might have. Below is the signature of an interface, it has only method signatures<\/p>\n\n\n\n
type name_of_interface interface{\n\/\/Method signature 1\n\/\/Method signature 2\n}<\/code><\/pre>\n\n\n\nLet’s understand the concept with the help of an example. Things will be more clear then. Let’s define an interface named animal. <\/strong>The animal<\/strong> interface has two methods breathe <\/strong>and walk<\/strong>. It defines only the method signatures and nothing else.<\/p>\n\n\n\ntype animal interface {\n breathe()\n walk()\n}<\/code><\/pre>\n\n\n\nA method signature would include<\/p>\n\n\n\n
- Name of the method<\/li><\/ul>\n\n\n\n
- Number of arguments and type of each argument<\/li><\/ul>\n\n\n\n
- Number of return values and type of each return value<\/li><\/ul>\n\n\n\n
With the above declaration, we created a new interface type i.e animal. <\/strong>It is ok to define a variable of animal<\/strong> type.<\/p>\n\n\n\nLet’s create a variable of animal<\/strong> interface type.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype animal interface {\n breathe()\n walk()\n}\n\nfunc main() {\n var a animal\n fmt.Println(a)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nnil<\/code><\/pre>\n\n\n\nAs seen in the above program it is ok to create a variable of interface type. It prints nil as the default zero value of an interface is nil.<\/p>\n\n\n\n
<\/span>Implementing an Interface<\/strong><\/span><\/h1>\n\n\n\nAny type which implements the breathe and walk method then it is said to implement the animal<\/strong> interface. So if we define a lion<\/strong> struct and implements the breathe and walk method then it will implement the animal interface.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype animal interface {\n breathe()\n walk()\n}\n\ntype lion struct {\n age int\n}\n\nfunc (l lion) breathe() {\n fmt.Println(\"Lion breathes\")\n}\n\nfunc (l lion) walk() {\n fmt.Println(\"Lion walk\")\n}\n\nfunc main() {\n var a animal\n a = lion{age: 10}\n a.breathe()\n a.walk()\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nLion breathes\nLion walk<\/code><\/pre>\n\n\n\nWe declare a variable of animal interface type<\/p>\n\n\n\n
var a animal<\/code><\/pre>\n\n\n\nThen we assign an instance of lion struct to it.<\/p>\n\n\n\n
a = lion{}<\/code><\/pre>\n\n\n\nAssigning an instance of lion<\/strong> struct to a variable of animal<\/strong> interface type works because lion<\/strong> struct implements both breathe<\/strong> and walk<\/strong> method of the animal. The type is not checked during this assignment, instead, it is enough to check that the type assigned does implement breathe<\/strong> and walk<\/strong> method. The concept is similar to duck typing, a lion<\/strong> can breathe<\/strong> and walk<\/strong> like an animal<\/strong> and hence it is an animal<\/strong>.<\/p>\n\n\n\nIf you notice that there is no explicit declaration that the lion type implements the animal interface. This brings a very important property related to interface – ‘Interface are implemented implicitly<\/p>\n\n\n\n
<\/span>Interface are implemented implicitly<\/strong><\/span><\/h1>\n\n\n\n<\/p>\n\n\n\n
There is no explicit declaration that a type implements an interface. In fact, in Go there doesn’t exist any “implements” <\/strong>keyword similar to Java. A type implements an interface if it implements all the methods of the interface.<\/p>\n\n\n\nAs seen above, It is correct to define a variable of an interface type and we can assign any concrete type value to this variable if the concrete type implements all the methods of the interface.<\/p>\n\n\n\n
There is no explicit declaration that says that lion<\/strong> struct implements the animal<\/strong> interface. During compilation, go notices that lion<\/strong> struct implements all methods of animal<\/strong> interface hence it is allowed. Any other type which implements all methods of the animal<\/strong> interface becomes of that interface type.<\/p>\n\n\n\nLet’s see a more complex example of another type implementing the animal interface. <\/p>\n\n\n\n
If we define a dog<\/strong> struct and it implements the breathe<\/strong> and walk<\/strong> method then it will also be an animal.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype animal interface {\n breathe()\n walk()\n}\n\ntype lion struct {\n age int\n}\n\nfunc (l lion) breathe() {\n fmt.Println(\"Lion breathes\")\n}\n\nfunc (l lion) walk() {\n fmt.Println(\"Lion walk\")\n}\n\ntype dog struct {\n age int\n}\n\nfunc (l dog) breathe() {\n fmt.Println(\"Dog breathes\")\n}\n\nfunc (l dog) walk() {\n fmt.Println(\"Dog walk\")\n}\n\nfunc main() {\n var a animal\n \n a = lion{age: 10}\n a.breathe()\n a.walk()\n \n a = dog{age: 5}\n a.breathe()\n a.walk()\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nLion breathes\nLion walk\nDog breathes\nDog walk<\/code><\/pre>\n\n\n\nBoth lion <\/strong>and dog <\/strong>implement the breathe and walk method hence they are of animal type and can correctly be assigned to a variable of interface type.<\/p>\n\n\n\nThe animal<\/strong> interface variable a was assigned a lion<\/strong> instance first and then the same variable was assigned a dog <\/strong>instance. So the type which interface variable refers to is dynamic. It dynamically holds a reference to the underlying type.<\/p>\n\n\n\nTwo important points to note:<\/p>\n\n\n\n
- The interface static check is done during compile time – means that if a type doesn’t implement all the methods of an interface, then assigning the type instance to a variable of that interface type will raise an error during compile time. Eg. on deleting the walk method defined on lion struct, below error will be raised during the assignment<\/li><\/ul>\n\n\n\n
cannot use lion literal (type lion) as type animal in assignment:<\/code><\/pre>\n\n\n\n