Interface Values
The value of the interface type and the interface value are two different concepts.
Go+ is a statically typed programming language, types are a compile time concept, so a type is not a value. The value of the type descriptor provides information about each type, such as its name and methods. In an interface value, the type component is represented by the appropriate type descriptor.
In Go+, interfaces variables are always initialize well-defined value as other type variables. The zero value for an interface has both its type and value components set to nil, for example:
The interface variable w has zero value. So both its dynamic type and its dynamic value are nil. In this case, the result of if w == nil is true. The interface variable r2 has non-zero value. Its dynamic type is *bytes.Reader and its dynamic value is nil. So the result of r2 == nil is false.
Interface values may be compared using == and !=. Two interface values are equal if both are nil, or if their dynamic types are identical and their dynamic values are equal according to the usual behavior of == for that type. Because interface values are comparable, they may be used as the keys of a map or as the operand of a switch statement.
However, if two interface values are compared and have the same dynamic type, but that type is not comparable (a slice, for instance), then the comparison fails with a panic. For example
In this respect, interface types are unusual. Other types are either safely comparable (like basic types and pointers) or not comparable at all (like slices, maps, and functions), but when comparing interface values or aggregate types that contain interface values, we must be aware of the potential for a panic. A similar risk exists when using interfaces as map keys or switch operands. Only compare interface values if you are certain that they contain dynamic values of comparable types.
When handling errors, or during debugging, it is often helpful to report the dynamic type of an interface value. For that, we use the fmt package’s %T verb:
Internally, fmt uses reflection to obtain the name of the int erface’s dynamic type.
Next example: The error Interface