Go Reference ยท Keywords and Statements
The type declaration
Reference for the Go type declaration: defined types versus aliases, underlying types, methods on new types, conversion rules and generic type declarations.
The type keyword introduces a new name for a type. There are two forms with different meaning. A defined type (type Celsius float64) creates a brand new type that has the same underlying type but its own identity and method set. An alias (type Celsius = float64) only gives a second name to an existing type; both names mean exactly the same type.
Defined types are the basis of Go's type safety: Celsius and Fahrenheit can both be float64 underneath yet cannot be mixed without an explicit conversion. Methods can be declared on defined types in the same package.
type Celsius float64 type Pair[K comparable, V any] struct { Key K; Val V } type Handler = func(string) error // alias
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) . TypeSpec = AliasDecl | TypeDef . AliasDecl = identifier [ TypeParams ] "=" Type . TypeDef = identifier [ TypeParams ] Type .
| Form | Description |
|---|---|
type T U |
Defined type. T has the underlying type of U but a new identity. It does not inherit U's methods (except promoted methods of embedded fields in a struct U). |
type T = U |
Alias. T and U are identical. Methods cannot be added to an alias of a type from another package. |
type T[P Constraint] ... |
Generic type with type parameters. Aliases may be generic since Go 1.24. |
| Local declaration | Types can be declared inside functions. They cannot have methods. |
Usage notes
- Conversions. A value converts between types with identical underlying types:
Celsius(f). Untyped constants adapt automatically (var c Celsius = 100). - Method sets. Define methods with a receiver of the defined type or a pointer to it. A defined type from an interface keeps the interface's methods; one from a struct loses the original type's methods.
- Enums. Go has no enum keyword; use a defined integer type with
iotaconstants and aStringmethod. - Aliases are for refactoring. They support gradual moves of a type between packages and shorter names.
byteandruneare built-in aliases ofuint8andint32;anyis an alias ofinterface{}. - Recursive types. A type may refer to itself through pointers, slices, maps, channels or functions, but not directly by value.
- Generic methods. Methods of generic types use the type's parameters in the receiver (
func (s *Stack[T]) Push) and cannot declare new type parameters.
Related
struct,interface,const,func, Type parameters- Lessons: Custom types, Generic types, Type conversion