Interfaces

interface is a custom type used to specify a set of one or more method signatures, and the interface is abstract, so you are not allowed to instantiate the interface.

But you are allowed to create a variable of an interface type, and that variable can be assigned a concrete type value that has the methods the interface requires. Or, in other words, the interface is a method collection in addition to being a custom type.


type Talker interface {
     speak(string) // method
}

type Parrot struct{}

// Parrot implements a Talker interface,
// Because it has all the methods it asks for.
func(Parrot) speak(string word) {
    fmt.Println(word)
}
                        
interfaces.go