liveBook · Manning

Type alias

As explained in Go in Action, Second Edition

A type alias gives an existing type an alternate name without creating a distinct type.

A type alias gives an existing Go type another name without creating a distinct type. Use one when a name clarifies how a value is being used but values should remain interchangeable with the original type. For example, byte is an alias for uint8, and rune is an alias for int32. section 3.1 section 5.1.1

Alias or distinct measurement type?

For a recipe measurement, `type Amount = float32` makes Amount another name for float32: an Amount and a float32 can be used interchangeably. By contrast, `type Magnitude float32` defines a distinct type. Converting a float32 to Magnitude must be explicit, and Magnitude can have its own methods. Choose the alias when a second name is enough; choose the defined type when the compiler should distinguish measurement values from ordinary numbers. section 5.1.1 section 5.1

How Go in Action, Second Edition covers it

How deep it goes
Defines `type A = B` and contrasts compiler behavior and methods with defined types; does not develop a larger alias implementation.
Examples
Uses byte and rune as built-in aliases, then contrasts an illustrative measurement alias with a distinct Magnitude type.
What it assumes
Assumes familiarity with Go primitive types and declarations while explaining the alias–defined type distinction directly.
What you can do after
Evaluate/understand when an alternate name suffices and when a distinct type provides compiler-enforced separation or methods.
How it connects
Connects aliases to primitive types, defined types, type conversion, methods, and domain modeling.

Also explained in

Compare how 6 books explain type alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.