Type definitions
July 20, 2023
Some of the minutiae we’ve gone over about underlying types, and type identity are finally starting to come together…
Type definitions
A type definition creates a new, distinct type with the same underlying type and operations as the given type and binds an identifier, the type name, to it.
TypeDef = identifier [ TypeParameters ] Type .
The new type is called a defined type. It is different from any other type, including the type it is created from.
type ( Point struct{ x, y float64 } // Point and struct{ x, y float64 } are different types polar Point // polar and Point denote different types ) type TreeNode struct { left, right *TreeNode value any } type Block interface { BlockSize() int Encrypt(src, dst []byte) Decrypt(src, dst []byte) }
There’s nothing especially surprising here. I think the most important thing here is to re-iterate that when creating a new type, it is, well, a new type. It has a different type identity, even though it may share an underlying type with other defined types. This matters for purposes of comparison, assignment, and method invocation, which we’ll consider tomorrow.
Quotes from The Go Programming Language Specification Version of December 15, 2022