Interface variables

February 9, 2023

Variables

… Variables of interface type also have a distinct dynamic type, which is the (non-interface) type of the value assigned to the variable at run time (unless the value is the predeclared identifier nil, which has no type). The dynamic type may vary during execution but values stored in interface variables are always assignable to the static type of the variable.

We haven’t really talked about interfaces yet, as that’s coming up in the next section of the spec about types. For now, I want to just make a couple points, to help avoid some common confusion about interfaces in Go.

Most important, an interface is its own type. It’s not some sort of type filter (something like that exists with Generics, which we’ll talk about in due course). This is important, because a lot of Go newcomers seem to think that an interface, and a type that implements an interface, ought to be interchangeable. They are not. Example:

var x interface{}  // The empty interface is implemented by all other types,
                   // since all types contain the complete list (of 0) methods
                   // in the interface

x = int(3) // This is valid; we've given our variable x, of type interface{},
           // a value of int(3)

var y int = x // This is not valid. Although x's underlying dynamic type is int,
              // x itself is of type interface{}, and not assignable to y which
              // is of type int.

x = string("foo") // Also valid; Now x's dynamic type is string. This obviously
                  // cannot be assigned to an int.

This seems to confuse people most often with regard to function calls. Let me demonstrate:

func add(a, b int) int {
	return a+b
}

var x interface{}

x = int(3)
var y = int(5)

sum := add(x, y) // Invalid; x is of type interface{}, while y is of type int

Let’s concluded today with some of the examples provided inline in the spec:

var x interface{}  // x is nil and has static type interface{}
var v *T           // v has value nil, static type *T
x = 42             // x has value 42 and dynamic type int
x = v              // x has value (*T)(nil) and dynamic type *T

Quotes from The Go Programming Language Specification, Version of January 19, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe