November 2018
Beginner to intermediate
348 pages
8h 32m
English
An interface is a set of methods. We can define an interface by listing out the methods it's expected to support. For example, consider the following interface:
var a interface { check() }
Here we are defining a to be a variable that has the type interface{ check() }. What on earth does that mean?
It means that you can put any value into a, as long as the value has a type that has a method called check().
Why is this valuable? It's valuable when considering multiple types that do similar things. Consider the following:
type complicatedEmail struct {...} func (e complicatedEmail) check() {...} func (e complicatedEmail) send(a string) {...} type simpleEmail string func (e simpleEmail) check() {...} func (e simpleEmail) send(a ...Read now
Unlock full access