In Go, type-level programming mostly means defining your own types on top of the built-in ones and giving them behaviour. Built-in types such as int, float64, string, bool, slices and maps are provided by the language. Custom types are declared with the type keyword, can have methods, and make the compiler catch mixups that the raw built-in types would allow.
Generics (Go 1.18+) add type parameters to this picture, but the everyday tool is the humble defined type.
Built-in types are predeclared in the universe scope. You cannot add methods to them, and you cannot change them.
| Group | Types |
|---|---|
| Boolean | bool |
| Integers | int, int8 to int64, uint, uint8 to uint64, uintptr, byte (alias of uint8), rune (alias of int32) |
| Floating point and complex | float32, float64, complex64, complex128 |
| Text | string |
| Composite | arrays, slices, maps, channels, pointers, functions, structs, interfaces |
| Special | error, any (alias of interface{}), comparable |
There are three ways to introduce a new name, and they behave differently.
A defined type (Celsius) is a different type from float64, so you cannot mix them without an explicit conversion. An alias (Temp) is identical to its target and is mainly used for gradual refactoring and moving types between packages.
You can attach methods to any defined type declared in your own package, including ones based on built-in types:
Implementing String() string satisfies fmt.Stringer, so fmt uses it automatically. The iota constants plus a defined type are Go's idiomatic enum.
The compiler treats UserID and OrderID as different types even if both are int64, which prevents argument mixups.
Untyped constants such as Cancel(7) still work, because a constant converts implicitly to a defined numeric type.
| Aspect | Built-in types | Custom types |
|---|---|---|
| Declared by | The language | You, with type |
| Methods | Not allowed | Allowed (in your package) |
| Mixing with the underlying type | Same type | Needs explicit conversion |
| Purpose | General data handling | Domain meaning, validation, behaviour |
| Performance | Baseline | Same as the underlying type; no runtime cost for a defined type |
Avoid wrapping a type without a reason; a thin type with no methods and no safety benefit only adds conversions.
No. A defined type has the same representation and the same speed as its underlying type. Conversions between them are free at run time.
type A B and type A = B?type A B creates a new, distinct type with its own method set. type A = B only adds another name for B.
int or string?Not directly. Define your own type, such as type MyInt int, and add methods to that.
No. Generics add type parameters to functions and types; you still define structs and named types for your domain.
Built-in types handle general data, while custom types attach meaning and behaviour. Use type with a struct or an underlying type for distinct, method-bearing types, an alias only for renaming, and generics when one algorithm must work across many types.