In Go, a named type is a type with its own identifier, such as int, string or a type you declare with type Age int. An anonymous type, more precisely a type literal, is a type written inline without a name, such as []int, map[string]bool, struct{ X, Y int } or func(int) int. Go's spec calls these unnamed types.
The difference matters because only named types can have methods, and it changes when two values can be assigned to each other. This guide explains both with examples for current Go (1.22 and later).
Predeclared types (int, string, bool, error) and any type you create with a type declaration are named. Types built from other types with a literal are unnamed.
Every type has an underlying type. For Age it is int; for a type literal it is the literal itself.
type Name Underlying creates a new, distinct type. It shares the memory layout of the underlying type but is not interchangeable with it.
The compiler stops you from mixing Celsius and Fahrenheit, which prevents unit mix-ups. You must convert explicitly: Fahrenheit(temp).
Do not confuse this with a type alias. type Age = int makes Age another name for int, with the same identity, and is mostly used for refactoring. Since Go 1.24 aliases can also have type parameters.
Unnamed types are handy for one-off data. Anonymous structs are common in table-driven tests and for quick JSON shapes:
Use a named type instead when the shape is reused, needs methods, or appears in a function signature or exported API. A named type documents intent and gives you a place to attach behaviour.
You can declare a method only on a named type defined in the same package. You cannot add methods to []int or to int directly:
Here IntList is a named type whose underlying type is the unnamed []int. This pattern is how packages like sort and net/http attach behaviour to simple types (http.HandlerFunc is a named function type with a ServeHTTP method).
A value can be assigned to a variable of a type T when their types are identical, or when they have identical underlying types and at least one of them is not a named type. That makes this legal:
But two different named types do not mix, even with the same underlying type:
Two unnamed struct types are identical only if they have the same fields, in the same order, with the same names, types and tags.
Named types are not limited to structs and numbers. A named function type lets you attach methods to a function, which is how http.HandlerFunc turns an ordinary function into an http.Handler:
Interface types follow the same rule. type Shape interface { Area() float64 } is a named interface, while interface{ Area() float64 } written inline is an unnamed one. Both work with the same implementations, but the named form is easier to reuse and document.
Not in the same sense. Go has unnamed type literals, including anonymous structs, but you declare the type explicitly where you use it. There is no compiler-generated type from an object initialiser.
An alias is another name for an existing type and has the same identity. type Age = int and int are fully interchangeable, while type Age int creates a new type.
No. Define a named type based on it, such as type MyInt int, and add methods to that.
Use it for local, short-lived data such as test cases. Switch to a named struct as soon as the shape is shared or needs methods.
Named types carry a name, can have methods and are distinct from each other even with equal underlying types. Unnamed types are inline literals that are identical when their structure matches. Prefer named types for domain concepts and public APIs, and keep anonymous structs for local, throwaway data.