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. ...

Get Go: Design Patterns for Real-World Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.