Skip to main content

Interfaces

Interfaces in Golang are similar to duck typing. There is no keyword to describe that a struct implements an interface. But looking at the available methods on the type, the compiler decides.

Interface as function arguments

If a function has an interface type as the argument, then the type implementing the interface is copied to the interface variable.

  1. If the pointer is sent in the function call, then the pointer of the implemented struct is copied inside the interface variable.
  2. If the value is sent, then the value is copied into the interface variable.
Interface value isn't mutable

Once the value is set, then there is no way this value or the reference set in the interface can be changed.