arthrop0d

Brief articles on varied topics.

Go Interface Values, Method Sets, and Typed Nil Pointers

golangsoftware engineering

An interface value in Go contains a dynamic type and a dynamic value. An interface is nil only when it contains neither. This is why an interface holding a typed nil pointer is not itself nil.

Interfaces solve a decoupling problem: code can depend on the operations an object provides without depending on its concrete type. Without interfaces, a function that accepts several implementations would need to name each concrete type, use type switches, or duplicate logic.

The type-and-value pair

Consider this interface and two implementations:

type Notifier interface {
\tNotify(string) error
}

type EmailNotifier struct{}

func (EmailNotifier) Notify(message string) error {
\treturn nil
}

type SMSNotifier struct{}

func (SMSNotifier) Notify(message string) error {
\treturn nil
}

A variable of type Notifier can hold either implementation:

var n Notifier = EmailNotifier{}

Conceptually, n contains two pieces of information:

The interface's static type is still Notifier. That is the type the compiler uses when checking what operations are available through n. The dynamic type is the concrete type stored inside it, which Go uses for method dispatch at runtime.

The language definition describes this behavior in terms of a type and a value. A particular Go runtime may use a pair of machine words or another internal representation; code should rely on the semantic model, not on the layout.

When n.Notify("hello") runs, Go uses the dynamic type's implementation of Notify. The caller does not need to know whether n contains an email sender, an SMS sender, or a test double.

How a type satisfies an interface

Go uses implicit interface satisfaction: a type satisfies an interface when its method set contains every method required by that interface. There is no declaration such as implements Notifier.

A method set is the collection of methods available for a particular type in the context where Go checks interface satisfaction. Receiver choice matters:

type Report struct{}

func (Report) Write() error {
\treturn nil
}

func (*Report) Close() error {
\treturn nil
}

The method set of Report contains Write, because Write has a value receiver. The method set of *Report contains both Write and Close: a pointer type gets methods declared with either value or pointer receivers.

Therefore, this interface is satisfied only by *Report:

type WriterCloser interface {
\tWrite() error
\tClose() error
}

var wc WriterCloser = &Report{}

This would not compile:

// var wc WriterCloser = Report{}

Report does not have Close in its method set. Although Go often automatically takes the address of an addressable value when calling a pointer-receiver method, that convenience does not change the method set used for interface satisfaction.

This distinction explains many compiler errors that say a type is missing a method. Check both the method name and signature, then check whether the value or pointer form has the required method set. A method with the wrong parameter types, return types, or receiver does not satisfy the interface.

A nil interface versus an interface containing nil

An interface can be assigned nil directly:

var n Notifier
fmt.Println(n == nil) // true

Here, n has no dynamic type and no dynamic value. It is a nil interface. Calling a method through it generally causes a panic because there is no concrete implementation to invoke.

Now compare that with a typed nil pointer:

type Connection struct{}

func (*Connection) Notify(message string) error {
\treturn nil
}

var c *Connection = nil
var n Notifier = c

fmt.Println(n == nil) // false

The interface n contains:

The interface itself is therefore not nil. Its type information is present even though the pointer value is absent. The comparison n == nil asks whether the interface has no type and no value; it does not ask whether the value inside the interface is a nil pointer.

This commonly appears with the error interface:

type ValidationError struct {
\tMessage string
}

func (e *ValidationError) Error() string {
\treturn e.Message
}

func validate() error {
\tvar e *ValidationError = nil
\treturn e
}

err := validate()
fmt.Println(err == nil) // false

*ValidationError satisfies error because it has an Error() string method. Returning the typed nil pointer converts it to an error interface containing the type *ValidationError and a nil pointer value. Callers see a non-nil error.

If Error dereferences its receiver, using err may then panic. A method can deliberately support a nil receiver by checking it first:

func (e *ValidationError) Error() string {
\tif e == nil {
\t\treturn "validation failed"
\t}
\treturn e.Message
}

The safer convention is usually to return a plain nil error when there is no error, rather than returning a typed nil pointer.

When debugging an unexpected non-nil error, inspect both the interface's dynamic type and the pointer value it contains. “The pointer is nil” and “the interface is nil” are different facts.

The same rule applies to any interface, not just error: interfaces carry type information even when their contained pointer, map, slice, function, channel, or other nil-capable value is nil. Understanding that pair—and the method sets that allow the pair to be formed—makes interface assignment, dispatch, and nil checks predictable.

← All articles