Interfaces
About
Interfaces are useful as contracts, to state what fields or methods a struct should follow.
struct
can be seen as the implementation description of the object.
interface
can be seen as generic contracts, that structs can "implement".
Creating
Interfaces can have methods descriptions in them, as opposed to struct
that can only have the fields descriptions.
Interfaces with only one method
any interface
Similar to Typescript any
type, you have a generic interface that accepts anything with interface{}
or any
.
Using it
In Go you don't have to explicitly state that the structs Node
and Todo
have to implement saver
.
The struct just have to implement whatever the interface states.
In the example, since the structs have a Save()
method and state
field, it is enough.
Embedded interfaces
In the same way as struct
you may embbed interface,
Last updated