Type declaration
In Go, it is possible to bind a type to an identifier to create a new named type that can be referenced and used wherever the type is needed. Declaring a type takes the general format as follows:
type <name identifier> <underlying type name>
The type declaration starts with the keyword type
followed by a name identifier and the name of an existing underlying type. The underlying type can be a built-in named type such as one of the numeric types, a Boolean, or a string type as shown in the following snippet of type declarations:
type truth bool type quart float64 type gallon float64 type node string
Note
A type declaration can also use a composite type literal as its underlying type. Composite types include array, slice, map, and struct. This section focuses on non-composite types. For further details on composite types, refer to Chapter 7, Composite Types.
The following sample illustrates how named types work in their most basic forms. The...