Aliases vs definitions
type Count int defines a new named type with int as its underlying type. type Count = int is an alias: Count and int are the same type. Definitions need conversions; aliases do not.
Detailed Explanation
You want to stop passing raw int around for two different quantities: a user ID and a retry count. type UserID int and type Count int are definitions. The compiler will not pass a UserID where a Count is required. That is the point. type UserID = int would not give you that check; it would only be another name for int.
Definitions create a new type. Methods you attach go on that type: func (id UserID) String() string. Conversion to the underlying type is explicit: int(id). Conversion between two definitions that share an underlying type is also explicit: Count(id) compiles if both are based on int, and it is a deliberate hole you should not drive a truck through. Assignability still requires the same type.
Aliases are the same type. type Count = int means Count is int. You cannot attach methods to Count, because you would be attaching them to int. Aliases exist for migrations: rename a type across packages without breaking callers in one commit. byte is an alias of uint8. rune is an alias of int32. any is an alias of interface{}.
Underlying type is what ~ sees. Generics with ~int accept type Count int and reject type Count = string. Aliases disappear before that check: a Count alias of int is int.
Defined types do not inherit methods. type Buffer bytes.Buffer does not have Write. Embedding does: type Buffer struct{ bytes.Buffer } promotes Write. People coming from type aliases in TypeScript often expect methods to follow the name. In Go, methods follow the type identity.
What it costs. A definition is a one-word conversion at the boundary and a compile-time seam everywhere else. An alias costs nothing and buys no safety. Prefer a definition for IDs, enums-as-integers, and units (type Cents int64). Prefer an alias when you are renaming a public type and both names must mean the same thing during the rollout.
Code Examples
package main
import "fmt"
type UserID int
type Count int
func (id UserID) String() string { return fmt.Sprintf("user:%d", int(id)) }
func main() {
var id UserID = 7
// var c Count = id // compile error: different defined types
c := Count(id) // explicit conversion
fmt.Println(id, c)
}type ID = int // alias: same as int
func takesInt(n int) {}
func alias() {
var id ID = 3
takesInt(id) // legal: ID is int
}type Buffer struct {
bytes.Buffer // embedding, not alias — methods are promoted
}Common Gotchas
You cannot declare methods on an alias of a predeclared type
`type Count = int` plus `func (Count) Inc()` fails. Use a definition.
Two definitions with the same underlying type still convert
`Count(userID)` compiles. Wrap the value in a struct if you need a harder boundary.
`type T U` does not copy methods of `U`
embed `U` or wrap it and write forwarding methods.
`any` and `byte` are aliases
they are not definitions, which is why `[]byte` and `[]uint8` are the same type.