Interfaces

Table of Contents

Basics of interface

package io

// Writer is the interface that wraps the basic Write method.
type Writer interface {
    // Write writes len(p) bytes from p to the underlying data stream.
    // It returns the number of bytes written from p (0 <= n <= len(p))
    // and any error encountered that caused the write to stop early.
    // Write must return a non-nil error if it returns n < len(p).
    // Write must not modify the slice data, even temporarily.
    //
    // Implementations must not retain p.
    Write(p []byte) (n int, err error)
}
package fmt

// The String method is used to print values passed
// as an operand to any format that accepts a string
// or to an unformatted printer such as Print.
type Stringer interface {
    String() string
}
Combining interfaces like struct embedding
The empty interface, interface{}, stands for any type.
Type assertions (x.(T))
Type Switches (x.(type)

interface Values discussion

var w io.Writer // nil interface value

w = os.Stdout

w = new(bytes,Buffer)

var x interface{} = time.Now()

An interface value assigned by nil of some type is not nil.
Gotcha!