What is interface type in Golang


Interface in Go is a type which provides a way to specify the behavior of objects: if something can do this, then it can be used here.

So interface is a type. An interface type can have zero or many methods. If an object of other type implements these methods, then the object can be used as the interface type.

An interface can have zero method. All types satisfy the empty interface. That is, if you write a function and take the interface as a parameter, you can pass any value.

There is no inheritance or polymorphism in Golang, the interface type partially works in that way.

What is the difference between interface and class?

interface:

  • contains only methods, no method body, no data

abstract class

  • contains data and method and its implementation
  • in C++, a class have polymorphism, inheritance and overriding properties


[1] http://research.swtch.com/interfaces
[2] http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go




  


Comments:

Write a comment
Anonymous

Captcha image

Reload

Type the number you see in the image above: