Interfaces are named collections of method signatures. |
|
|
|
|
|
Here’s a basic interface for geometric shapes. |
|
For our example we’ll implement this interface on
|
|
To implement an interface in Go, we just need to
implement all the methods in the interface. Here we
implement |
|
The implementation for |
|
If a variable has an interface type, then we can call
methods that are in the named interface. Here’s a
generic |
|
Sometimes it’s useful to know the runtime type of an
interface value. One option is using a type assertion
as shown here; another is a type |
|
|
|
The |
|
|
|
|
To understand how Go’s interfaces work under the hood, check out this blog post. |
Next example: Enums.