Gold, an experimental Go local docs server, Go docs generation tool, and code reader. NEW!

-- show type implemention relations --

-- show code statistics --

-- smooth code view experiences --

-- and more... --

Go Type System Overview

This article will introduce all kinds of types in Go and the concepts regarding Go type system. It is hard to have a thorough understanding of Go, without knowing these fundamental concepts.

Concept: Basic Types

Built-in string type: string .

. Built-in boolean type: bool .

. Built-in numeric types: int8 , uint8 ( byte ), int16 , uint16 , int32 ( rune ), uint32 , int64 , uint64 , int , uint , uintptr . float32 , float64 . complex64 , complex128 .

Built-in basic types in Go have been also introduced in built-in basic types and basic value literals . For completeness of the current article, these built-in basic types are re-listed here. Note, byte is a built-in alias of uint8 , and rune is a built-in alias of int32 . We can also declare custom type aliases (see below). Except string types, Go 101 article series will not try to explain more on other basic types.

Concept: Composite Types

pointer types - C pointer alike.

struct types - C struct alike.

function types - functions are first-class types in Go.

container types: array types - fixed-length container types. slice type - dynamic-length and dynamic-capacity container types. map types - maps are associative arrays (or dictionaries). The standard Go compiler implements maps as hashtables.

channel types - channels are used to synchronize data among goroutines (the green threads in Go).

interface types - interfaces play a key role in reflection and polymorphism. Go supports the following composite types:

// Assume T is an arbitrary type and Tkey is // a type supporting comparison (== and !=). *T // a pointer type [5]T // an array type []T // a slice type map[Tkey]T // a map type // a struct type struct { name string age int } // a function type func(int) (bool, string) // an interface type interface { Method0(string) int Method1() (int, bool) } // some channel types chan T chan Non-defined composite types may be denoted as their respective type literals. Following are some literal representation examples of all kinds of non-defined composite types (non-defined types will be explained below). Comparable and incomparable types will be explained below.

Fact: Kinds of Types

Each of the above mentioned basic and composite types corresponds to one kind of types. Besides these kinds, the unsafe pointer types introduced in the unsafe standard package also belong to one kind of types in Go. So, up to now (Go 1.15), Go has 26 kinds of types.

Syntax: Type Definitions

(Type definition, or type definition declaration, initially called type declaration, was the only type declaration way before Go 1.9. Since Go 1.9, type definition has become one of two forms of type declarations. The new form is called type alias declaration, which will be introduced in the next section.)

type is a keyword. // Define a solo new type. type NewTypeName SourceType // Define multiple new types together. type ( NewTypeName1 SourceType1 NewTypeName2 SourceType2 ) In Go, we can define new types by using the following form. In the syntax,is a keyword. New type names must be identifiers. But please note that, type names declared at package level can't be init . (This is the same for the following introduced type alias names.) The second type declaration in the above example includes two type specifications. If a type declaration contains more than one type specification, the type specifications must be enclosed within a pair of () . a new defined type and its respective source type in type definitions are two distinct types.

two types defined in two type definitions are always two distinct types.

the new defined type and the source type will share the same underlying type (see below for what are underlying types), and their values can be converted to each other.

types can be defined within function bodies. Note, // The following new defined and source types // are all basic types. type ( MyInt int Age int Text string ) // The following new defined and source types are // all composite types. type IntPtr *int type Book struct{author, title string; pages int} type Convert func(in0 int, in1 bool)(out0 int, out1 string) type StringArray [5]string type StringSlice []string func f() { // The names of the three defined types // can be only used within the function. type PersonAge map[string]int type MessageQueue chan string type Reader interface{Read([]byte) int} } Some type definition examples:

Syntax: Type Alias Declarations

(Type alias declaration is one new kind of type declarations added since Go 1.9.)

As above mentioned, there are only two built-in type aliases in Go, byte (alias of uint8 ) and rune (alias of int32 ). They are the only two type aliases before Go 1.9.

= in each type alias declaration. type ( Name = string Age = int ) type table = map[string]int type Table = map[Name]Age Since Go 1.9, we can declare custom type aliases by using the following syntax. The syntax of alias declaration is much like type definition, but please note there is ain each type alias declaration. Type alias names must be identifiers. Like type definitions, type aliases can also be declared within function bodies. Name is an alias of string , so both denote the same type. The same applies to the other three pairs of type notations (either names or literals): Age and int

and table and map[string]int

and Table and map[Name]Age A type name (or literal) and its aliases all denote an identical type. By the above declarations,is an alias of, so both denote the same type. The same applies to the other three pairs of type notations (either names or literals): In fact, the literals map[string]int and map[Name]Age also denote the same type. So, the same, aliases table and Table also denote the same type. Note, although aliases table and Table denote the same type, Table is exported so it can be used by other packages but this does not apply to table . Type alias declarations are useful in refactoring some large Go projects, they are not intended for general purpose uses. We should use type definition declrations in general programming.

Concept: Defined Types vs. Non-Defined Types

A defined type is a type defined in a type definition.

All basic types are defined. A non-defined type must be a composite type.

C and type literal []string both represent the same non-defined types, but type A and type alias B both represent the same defined type. type A []string type B = A type C = []string In the following example. type aliasand type literalboth represent the same non-defined types, but typeand type aliasboth represent the same defined type.

Concept: Named Types vs. Unnamed Types

Before Go 1.9, the terminology named type is defined accurately in Go specification. A named type was defined as a type who is represented by an identifier. Along with the type alias feature introduced in Go 1.9, this terminology is removed from Go specification as well, for it may cause some confusions in explaining and understanding some Go concepts. For example, some type names might denote unnamed types (such as the alias C , which is shown in the last section, denotes an unnamed type []string ). To avoid causing such confusions, since Go 1.9, a new terminology defined type is introduced to fulfill the blank by removing the old named type terminology. However, this change brings An alias will never be called as a type, though we may say it denotes/represents a type.

The terminology named type is viewed as an exact equivalence of defined type . (And unnamed type exactly means non-defined type .) In other words, when it says "a type alias T is a named type", it actually means the type represented by the alias T is a named type. If T represents an unnamed type, we should never say T is a named type, even if the alias T itself has a name.

is viewed as an exact equivalence of . (And exactly means .) In other words, when it says "a type alias is a named type", it actually means the type represented by the alias is a named type. If represents an unnamed type, we should never say is a named type, even if the alias itself has a name. When we mention a type name, it might be the name of a defined type or the name of a type alias. To avoid causing such confusions, since Go 1.9, a new terminologyis introduced to fulfill the blank by removing the oldterminology. However, this change brings some embarrassing situations , and causes some inconveniences in explaining some concepts . To avoid these new problems, Go 101 articles try to follow several principles:

Concept: Underlying Types

for built-in types, the respective underlying types are themselves.

for the Pointer type defined in the unsafe standard code package, its underlying type is itself. (At least we can think so. In fact, the underlying type of the unsafe.Pointer type is not well documented. We can also think the underlying type is *T , where T represents an arbitrary type.)

type defined in the standard code package, its underlying type is itself. (At least we can think so. In fact, the underlying type of the type is not well documented. We can also think the underlying type is , where represents an arbitrary type.) the underlying type of a non-defined type, which must be a composite type, is itself.

in a type declaration, the newly declared type and the source type have the same underlying type. Examples: // The underlying types of the following ones are both int. type ( MyInt int Age MyInt ) // The following new types have different underlying types. type ( IntSlice []int // underlying type is []int MyIntSlice []MyInt // underlying type is []MyInt AgeSlice []Age // underlying type is []Age ) // The underlying types of []Age, Ages, and AgeSlice // are all the non-defined type []Age. type Ages AgeSlice In Go, each type has an underlying type. Rules:Examples: How can an underlying type be traced given a user declared type? The rule is, when a built-in basic type or a non-defined type is met, the tracing should be stopped. Take the type declarations shown above as examples, let's trace their underlying types. MyInt → int Age → MyInt → int IntSlice → []int MyIntSlice → []MyInt → []int AgeSlice → []Age → []MyInt → []int Ages → AgeSlice → []Age → []MyInt → []int In Go, types whose underlying types are bool are called boolean types ;

are called ; types whose underlying types are any of the built-in integer types are called integer types ;

; types whose underlying types are either float32 or float64 are called floating-point types ;

or are called ; types whose underlying types are either complex64 or complex128 are called complex types ;

or are called ; integer, floating-point and complex types are also called numeric types ;

; types whose underlying types are string are called string types. In Go,

The concept of underlying type plays an important role in value conversions, assignments and comparisons in Go.

Concept: Values

An instance of a type is called a value, of the type. A type may have many values, one of them is the zero value of the type. Values of the same type share some common properties.

Each type has a zero value, which can be viewed as the default value of the type. The predeclared nil identifier can used to represent zero values of slices, maps, functions, channels, pointers (including type-unsafe pointers) and interfaces. For more information on nil , please read nil in Go.

There are several kinds of value representation forms in code, including literals, named constants, variables and expressions, though the former three can be viewed as special cases of the latter one.

A value can be typed or untyped.

All kinds of basic value literals have been introduced in the article basic types and basic value literals. There are two more kinds of literals in Go, composite literals and function literals.

Function literals, as the name implies, are used to represent function values. A function declaration is composed of a function literal and an identifier (the function name).

Composite literals are used to represent values of struct types and container types (arrays, slices and maps), Please read structs in Go and containers in Go for more details.

There are no literals to represent values of pointers, channels and interfaces.

Concept: Value Parts

At run time, many values are stored somewhere in memory. In Go, each of such values has a direct part. However, some of them have one or more indirect parts. Each value part occupies a continuous memory segment. The indirect underlying parts of a value are referenced by its direct part through pointers.

The terminology value part is not defined in Go specification. It is just used in Go 101 to make some explanations simpler and help Go programmers understand Go types and values better.

Concept: Value Sizes

When a value is stored in memory, the number of bytes occupied by the direct part of the value is called the size of the value. As all values of the same type have the same value size, we often call the same value size of a type as the size of the type.

We can use the Sizeof function in the unsafe standard package to get the size of any value.

Go specification doesn't specify value size requirements for non-numeric types. The requirements for value sizes of all kinds of basic numeric types are listed in the article basic types and basic value literals.

Concept: Base Type of a Pointer Type

For a pointer type, assume its underlying type can be denoted as *T in literal, then T is called the base type of the pointer type.

More information on pointer types and values can be found in the article pointers in Go.

Concept: Fields of a Struct Type

Book has three fields, author , title and pages . struct { author string title string pages int } A struct type consists a collection of member variable declarations. Each of the member variable declarations is called "field" of the struct type. For example, the following struct typehas three fields,and

More information on struct types and values can be found in the article structs in Go.

Concept: Signature of Function Types

The signature of a function type is composed of the input parameter definition list and the output result definition list of the function.

The function name and body are not parts of a function signature. Parameter and result types are important for a function signature, but parameter and result names are not important.

Please read functions in Go for more details on function types and function values.

Concept: Method and Method Set of a Type

In Go, some types can have methods. Methods can also be called member functions. The method set of a type is composed of all the methods of the type.

Concept: Dynamic Type and Dynamic Value of an Interface Value

Interface values are values whose types are interface types.

Each interface value can box a non-interface value in it. The value boxed in an interface value is called the dynamic value of the interface value. The type of the dynamic value is called the dynamic type of the interface value. An interface value boxing nothing is a zero interface value. A zero interface value has neither a dynamic value nor a dynamic type.

An interface type can specify zero or several methods, which form the method set of the interface type.

If the method set of a type, which is either an interface type or a non-interface type, is the super set of the method set of an interface type, we say the type implements the interface type.

For more about interface types and values, please read interfaces in Go.

Concept: Concrete Value and Concrete Type of a Value

For a (typed) non-interface value, its concrete value is itself and its concrete type is the type of the value.

A zero interface value has neither concrete type nor concrete value. For a non-zero interface value, its concrete value is its dynamic value and its concrete type is its dynamic type.

Concept: Container Types

Arrays, slices and maps can be viewed as formal container types.

Sometimes, string and channel types can also be viewed as container types informally.

Each value of a container type has a length, either that container type is a formal one or an informal one.

More information on formal container types and values can be found in the article containers in Go.

Concept: Key Type of a Map Type

If the underlying type of a map type can be denoted as map[Tkey]T , then Tkey is called the key type of the map type. Tkey must be a comparable type (see below).

Concept: Element Type of a Container Type

For an array type, if its underlying type is [N]T , then its element type is T .

, then its element type is . For a slice type, if its underlying type is []T , then its element type is T .

, then its element type is . For a map type, if its underlying type is map[Tkey]T , then its element type is T .

, then its element type is . For a channel type, if its underlying type is chan T , chan<- T or <-chan T , then its element type is T .

, or , then its element type is . The element type of any string type is always byte (a.k.a. uint8 ). The types of the elements stored in values of a container type must be identical. The identical type of the elements is called the element type of the container type.

Concept: Directions of Channel Types

A channel value which is both sendable and receivable is called a bidirectional channel. Its type is called a bidirectional channel type. The underlying types of bidirectional channel types are denoted as chan T in literal.

in literal. A channel value which is only sendable is called a send-only channel. Its type is called a send-only channel type. Send-only channel types are denoted as chan<- T in literal.

in literal. A channel value which is only receivable is called a receive-only channel. Its type is called a receive-only channel type. Receive-only channel types are denoted as <-chan T in literal. Channel values can be viewed as synchronized first-in-first-out (FIFO) queues. Channel types and values have directions.

More information on channel types and values can be found in the article channels in Go.

Fact: Types Which Support or Don't Support Comparisons

== and != operators) between values of the following types: slice types

map types

function types

any struct type with a field whose type is incomparable and any array type which element type is incomparable. Currently (Go 1.15), Go doesn't support comparisons (with theandoperators) between values of the following types: Above listed types are called incomparable types. All other types are called comparable types. Compilers forbid comparing two values of incomparable types. Note, incomparable types are also called as incomparable types in many articles. The key type of any map type must be a comparable type. We can learn more about the detailed rules of comparisons from the article value conversions, assignments and comparisons in Go.

Fact: Object-Oriented Programming in Go

methods in Go.

implementations in Go.

type embedding in Go. Go is not a full-featured object-oriented programming language, but Go really supports some object-oriented programming styles. Please read the following listed articles for details:

Fact: Generics in Go

Up until now (Go 1.15), the generic functionalities in Go are limited to built-in types and functions. Custom generics are still in draft phase now. Please read built-in generics in Go for details.