Go Interfaces: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
=Overview=
=Overview=


An interface is a type declaration that defines a ''method set''. A method set is a list of methods a type must have in order to ''implement'' the interface. Interface type instances can be used as arguments to functions, an interface instance can be passed as argument to a function after declaring it as such in the function signature.
An interface is a type declaration that defines a ''method set''.  


A type (<tt>struct</tt>, <font color=red>anything else?</font>) implements an interface implicitly, by doing nothing else but exposing all methods from the interface's method set. "Exposing" in this context means the methods in question [[Go_Concepts_-_Functions#Receivers|have been declared to use the type in question as receiver]]. This is called [[Go Concepts - The Type System#Duck_Typing|duck typing]]. There is no "implements" or "extends" keyword in Go.
A method set is a list of methods a type must have in order to ''implement'' the interface. A type (<tt>struct</tt>, <font color=red>anything else?</font>) implements an interface implicitly, by doing nothing else but exposing all methods from the interface's method set. "Exposing" in this context means the methods in question [[Go_Concepts_-_Functions#Receivers|have been declared to use the type in question as receiver]]. This is called [[Go Concepts - The Type System#Duck_Typing|duck typing]]. There is no "implements" or "extends" keyword in Go.
 
 
 
 
Interface type instances can be used as arguments to functions, an interface instance can be passed as argument to a function after declaring it as such in the function signature.


<font color=red>
<font color=red>

Revision as of 17:37, 30 March 2016

Internal

Overview

An interface is a type declaration that defines a method set.

A method set is a list of methods a type must have in order to implement the interface. A type (struct, anything else?) implements an interface implicitly, by doing nothing else but exposing all methods from the interface's method set. "Exposing" in this context means the methods in question have been declared to use the type in question as receiver. This is called duck typing. There is no "implements" or "extends" keyword in Go.



Interface type instances can be used as arguments to functions, an interface instance can be passed as argument to a function after declaring it as such in the function signature.

  • Interfaces are not types?
  • Can only structs be interfaces, or there are other things that can be interfaces?

Declaration

The interface declaration is introduced by the type keyword, to indicated that this is a user-defined type, followed by the interface name and the keyword interface. Unlike in the struct's case, we don't define fields but a method set.

type MyInterface interface {
     functionName1() return_type
     functionName2() return_type
     ...
}

Initialization