Overview of the Type System of Go Language

Posted by imnu on Mon, 06 May 2019 15:25:03 +0200

This article will introduce various concepts in go language and go type system. Without knowing these concepts, it will be difficult to understand go language.

Concept: Basic Types

The built-in types in go have been introduced in Built-in Basic Types and Values. For the sake of completeness of this article, the built-in basic types are listed here again.

  • Built-in string type: string.
  • Built-in Boolean type: bool.
  • Built-in numeric types: int, int8, uint8, int16, uint16, int32, uint32, int64, uint64, uint, uintprt,
    float32,float64, complex64,complex128.
    In addition to string types, the Go101 article system will not explain other basic types.

Concept: Combination Type

Go supports the following combination types:

  • Pointer type
  • Structural type
  • Function type
  • Container types: arrays, slices, map s
  • channel: Used to synchronize data between goroutine s
  • interface: plays an important role in reflection and polymorphism.

Compound types can be represented as their respective types of text. Here are some examples of literal representations of various 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<- T
<-chan T

Fact: Various types

Each of the above basic and composite types corresponds to one type. In addition to these types, the unsafe pointer type introduced in the unsafe package also belongs to one type.
So far (go1.12) go has 26 types.

Grammar: Type Definition

In go language, we can define new types in the following way. Grammatically, type is a key word.

type NewTypeName SourceType
type (
  NewTypeName1 SourceType1
  NewTypeName2 SourceType2
)

The new type name must be an identifier.
The second type declaration in the preceding example includes two types of specifications. If a type declaration contains more than one type specification, the type specification must be included in a pair of ().
Be careful:

  • The newly defined type and its corresponding original type belong to two completely different types in type definition.
  • Types defined on two types definitions are always different types
  • The newly defined type and source type will share the same underlying type, and their values can be converted to each other.
  • Types can be defined within functions

Some examples of type definitions:

// 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}
}

Grammar: Type alias declaration

type (
Name = string
Age = int 
)
type table = map[string]int
type Table = map[Name]Age

Concept: Defined and undefined types

A defined type is defined in a type definition or type alias.
All basic types are defined. A non-defined type must be a combination type.
In the following example, alias type C and type text [] string are both undefined types, but types A and B are both defined types.

type A []string
type B = A
type C = []string

Concept: Named type and anonymous type

In go language:

  • If a type has a name, the name must be an identifier and not an empty identifier. This type is called a named type. All basic types are named types. When a type declaration is an identifier that is not empty, it is also named types.

Concept: Subject type

In go language, each type has an object type. The rules are:

  • For built-in types, their underlying type is themselves
  • The type of unsafe pointer's tag is unsafe.Pointer, which is defined in the standard library.
  • An anonymous object type is itself
  • In a type declaration, the type of the new declaration has the same underlying type as the source type.

For example:

// 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 Ages and AgeSlice are both []Age.
type Ages AgeSlice

Given the type of a declaration, how do you track its subject type? The rule is when you encounter a build-in primitive type, unsafe.Pointer or anonymous type, tracing is terminated. As an example of the above type declarations, let's trace the type of their subject.

MyInt → int
Age → MyInt → int
IntSlice → []int
MyIntSlice → []MyInt → []int
AgeSlice → []Age → []MyInt → []int
Ages → AgeSlice → []Age → []MyInt → []int

In go language:

  • Boolean type is the type whose subject type is bool.
  • The type of subject is built-in integer and becomes integer
  • Subject type is float32,float64 is float type
  • Subject type is complex64,complex128 is complex type
  • Integers, floating-point numbers and complex numbers are also considered numeric types.
  • The type whose underlying type is string is called string type.

Concept: Value

An instance of a type is treated as a value of that type.
Each type has a value of 0.

Concept: Value components

Each value component occupies a continuous segment of memory, and the indirect target component is referenced by its direct component through a pointer.

At runtime, many values are in memory. In go, each of these values has a direct component. However, some have one or more indirect components.

Topics: Go