Member-only story
Go for Developers: Defined Types, Pointers and Functions
Introduction
In the previous part we covered the most common data structures in Go: arrays, slices, maps, and structs. We declared them using Type Literals, which are used to create Unnamed Types. Type Literals have their trade-offs, which will be discussed later in this article. This article will cover Defined Types, Pointers, and Functions.
Defined Types, are part of Named Types. You can look at the top-level split in the Basic Types section in the article.
Not a member yet? Become a member or read the full version in my Tech Notes.
Defined Types
In Go, defined types are the types you create yourself using the type keyword. Defined types prevent accidental misuse of semantically different values. A type definition creates a new, distinct type that is different from any other type, even if they share the same underlying structure:
type UserID int32
type ProductID int32
var u UserID = 1
var p ProductID = 1
// compile error: mismatched types
fmt.Println(u == p)By treating UserID and ProductID as different types, Go prevents you from accidentally passing a product's ID into a function that expects a user's ID.
