Go: Interfaces Cheat Sheet

Syntax

Define an interface

type interface_name interface {
   method_name1 [return_type]
   ...
   method_namen [return_type]
}

Define a struct

type struct_name struct {
   /* variables */
}

Implement interface methods

func (struct_name_variable struct_name) method_name1() [return_type] {
   /* method implementation */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
   /* method implementation */
}

Leave a Reply

Your email address will not be published. Required fields are marked *